2010-11-28 88 views
1

這裏是陣列使用JavaScript/jQuery來改變在按鈕的div文字點擊

var copyText= [ 
'this is the first line', 
'Simply put, the second line', 
'impressed by the third line.' 
    ]; 

這些不工作...

$("#thisbutton").click(function() { 
$("#thetext").innerHTML("meow meow"); 
}); 

$("#thisbutton").click(function() { 
$("#thetext").innerHTML(copyText[1]); 
}); 

$("#thisbutton").click(function() { 
$("#thetext").text(copyText[1]); 
}); 


$("#thisbutton").click(function() { 
$("#thetext").html(copyText[1]); 
}); 

我失蹤了什麼? THKS。

+0

請公佈源代碼的其他頁面上。另外,你可能想要把整個東西包裝在$(function(){// code here})中; – 2010-11-28 03:44:51

+0

謝謝,是的,這就是它。怎麼樣href和一個鏈接,如何可以訪問一個javascript數組? <!DOCTYPE HTML> <腳本類型= 「文本/ JavaScript的」> $(函數(){ VAR htmlLinks = [ 'http://cnn.com', ' http://youtube.com', 'http://facebook.com' ]; }); htmlLinks[0];" target="_blank"> link1 \ htmlLinks[1];" target="_blank"> link2 \ htmlLinks[2];" target="_blank"> link3 windsurf88 2010-11-28 16:33:30

回答

3

首先,innerHTML是本地DOMElement屬性,而不是jQuery方法。 jQueryhtml方法是等價的。

其次,你是不是在ready處理包裹的:

試試這個:

$(function() { 
    var copyText= [ 
    'this is the first line', 
    'Simply put, the second line', 
    'impressed by the third line.' 
    ]; 
    $("#thisbutton").click(function() { 
    $("#thetext").text(copyText[1]); 
    }); 

}); 

jsFiddle example

+0

無法獲取你的榜樣工作? – windsurf88 2010-11-28 16:02:43

+0

<!DOCTYPE HTML> <腳本類型= 「文本/ JavaScript的」> $(函數(){ VAR的copytext = [ '這是第一行', 「只需,第二行' '給第三行留下深刻印象' ]; $(「#thisbutton」)。點擊(function(){(「#thetext」).text(copyText [1]); }); });

Hello
<! -
pp
- > 如果使用 「喵喵」 – windsurf88 2010-11-28 16:04:02

1

這個工作對我來說...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
</head> 
<body> 
    <div id="thetext"> 

    </div> 
    <input type="button" id="thisbutton" value="Click" /> 
<script src="Scripts/jquery-1.4.2.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(function() { 
     $("#thisbutton").click(function() { $("#thetext").html("meow meow"); }); 
    }); 
</script> 
</body> 
</html>