2010-11-28 109 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> 
相关问题