开发者

jquery and click function

开发者 https://www.devze.com 2023-01-10 23:03 出处:网络
$(document).ready(function(){ $(\"#changeText\").click(function() { 开发者_高级运维 $(\"#textBox\").html(\"text text\");
$(document).ready(function(){ $("#changeText").click(function() { 开发者_高级运维 $("#textBox").html("text text"); }); });

How make that if i push on #changeText twice or more, i get two (or more) text text


don't use .html() which overwrites the entire innerHTML structure.

To append new html markup use .append() or .appendTo()

$(document).ready(function(){
  $("#changeText").click(function() {
      $("#textBox").append("<div>text text</div>");
  });
});

or in a more jQuery'ish chaining way:

$('<div>text text</div>').appendTo($('#textBox')); // .css().fadeOut().animate().etc()

Ref.: .append(), .appendTo()


You need to create div inside #textBox and then

$("#changeText").click(function() {
    $("#textBox div").append("text");
});

EDIT:

$("#changeText").click(function() {
    if ( $("#textBox div").length()>0)
          $("#textBox div").append("text");
    else
         $("#textBox").append("<div>text</div>");
});
0

精彩评论

暂无评论...
验证码 换一张
取 消