課程
/前端開發(fā)
/JavaScript
/JavaScript進階篇
為什么實現(xiàn)不了button的onclick這個屬性
2016-10-28
源自:JavaScript進階篇 9-16
正在回答
大概猜你的想法是什么:這是添加了一句window.open();可以實現(xiàn)。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
</head>
<body>
<script type="text/javascript">
var main = document.body;
function haha(x,y)
{
? ? var a=document.createElement("a");
? ? a.setAttribute("href",x);
? ? window.open("http://idcbgp.cn");
? ? a.innerHTML=y;
? ? return a;
? ? }
? ? var b=document.createElement("input");
? ? b.setAttribute("type","button");
? ? b.setAttribute("name","haha");
? ? b.setAttribute("value","anniu");
? ? b.setAttribute("onclick","haha('http://idcbgp.cn','慕課網(wǎng)')");
main.appendChild(b);
</script>?
</body>
</html>
?//你這個onclick事件時設置成功了的,不過你點擊之后,只是創(chuàng)建了一個連接,然而并沒有打開這個鏈接(不打開鏈接自然就不會有反應,而且你也沒有添加到文檔里面,所以看不出什么),我只加了一句window.open,點擊按鈕之后就會打開網(wǎng)頁了。 ?
//望采納
哦gqgq_627 提問者
一樓說得很好,不過直接加了一個window.open();這樣的話,創(chuàng)建的元素a就沒意義了,所以我覺得還是把創(chuàng)建的元素添加到文檔里面比較好。
function haha(x,y){??? var a=document.createElement("a");??? a.setAttribute("href",x);??? a.innerHTML=y;?? main.appendChild(a);??? }
舉報
本課程從如何插入JS代碼開始,帶您進入網(wǎng)頁動態(tài)交互世界
1 回答這樣寫哪里有問題,求解!
3 回答請問我這個哪里有問題?
2 回答請問這樣寫哪里錯了?
2 回答請問這樣寫刪除代碼哪里有錯
3 回答請問一下我這樣寫法問題在哪里?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關注慕課網(wǎng)微信公眾號
2016-10-29
大概猜你的想法是什么:這是添加了一句window.open();可以實現(xiàn)。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無標題文檔</title>
</head>
<body>
<script type="text/javascript">
var main = document.body;
function haha(x,y)
{
? ? var a=document.createElement("a");
? ? a.setAttribute("href",x);
? ? window.open("http://idcbgp.cn");
? ? a.innerHTML=y;
? ? return a;
? ? }
? ? var b=document.createElement("input");
? ? b.setAttribute("type","button");
? ? b.setAttribute("name","haha");
? ? b.setAttribute("value","anniu");
? ? b.setAttribute("onclick","haha('http://idcbgp.cn','慕課網(wǎng)')");
main.appendChild(b);
</script>?
</body>
</html>
?//你這個onclick事件時設置成功了的,不過你點擊之后,只是創(chuàng)建了一個連接,然而并沒有打開這個鏈接(不打開鏈接自然就不會有反應,而且你也沒有添加到文檔里面,所以看不出什么),我只加了一句window.open,點擊按鈕之后就會打開網(wǎng)頁了。 ?
//望采納
2016-11-02
一樓說得很好,不過直接加了一個window.open();這樣的話,創(chuàng)建的元素a就沒意義了,所以我覺得還是把創(chuàng)建的元素添加到文檔里面比較好。
function haha(x,y)
{
??? var a=document.createElement("a");
??? a.setAttribute("href",x);
??? a.innerHTML=y;
?? main.appendChild(a);
??? }
2016-10-29