admin管理员组

文章数量:1296875

here is the array

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

These don't work ...

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

or

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

or

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


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

What I am missing? thks.

here is the array

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

These don't work ...

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

or

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

or

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


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

What I am missing? thks.

Share Improve this question edited Nov 28, 2010 at 5:22 Jacob Relkin 163k33 gold badges351 silver badges321 bronze badges asked Nov 28, 2010 at 3:42 windsurf88windsurf88 1091 gold badge4 silver badges13 bronze badges 2
  • Please post the rest of the source code on the page. Also, you probably want to wrap the whole thing in $(function(){ //code here }); – Stefan Mai Commented Nov 28, 2010 at 3:44
  • thanks, yes thats it. what about href and a links, how can a javascript array be accessed? <!DOCTYPE html> <html> <head> </head> <body> <script type="text/javascript"> $(function() { var htmlLinks= [ 'cnn.', 'youtube.', 'facebook.' ]; }); </script> <a href="<script type="text/javascript">htmlLinks[0];</script>" target="_blank"> link1 </a> \ <a href="<script type="text/javascript">htmlLinks[1];</script>" target="_blank"> link2 </a> \ <a href="<script type="text/javascript">htmlLinks[2];</script>" target="_blank"> link3 </a> </body> </html> – windsurf88 Commented Nov 28, 2010 at 16:33
Add a ment  | 

3 Answers 3

Reset to default 6

First of all, innerHTML is a native DOMElement property, not a jQuery method. jQuery's html method is the equivalent.

Secondly, you are not wrapping this in a ready handler:

Try this:

$(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

This worked for me...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/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>

If you want to replace the plete div try replaceWith() jquery function

 $("#thetext").replaceWith("htmldata");

Reference: http://api.jquery./replaceWith/

本文标签: Using JavascriptJquery to change text in div upon button clickStack Overflow