javascript - Add some string inside div tag using jQuery - Stack Overflow

I'm just trying to add some string or HTML tag inside div #OL_Icon_63 using jQuery .append(), here

I'm just trying to add some string or HTML tag inside div #OL_Icon_63 using jQuery .append(), here's the example of code :

$(document).ready(function() {
    $('#OL_Icon_63').append('HELLO WORLD');
});

But that's code won't work for me, I try to change the code like this :

$(document).ready(function() {
    $("body").click(function() {
       $('#OL_Icon_63').append('HELLO WORLD');
    });
});

And

$(document).ready(function() {
    $("body").hover(function() {
       $('#OL_Icon_63').append('HELLO WORLD');
    });
});

Two kinds of code work for me where I use .hover() and .click() after body tag, but i basically can not use the function like .append() or anything else without adding .hover() or .click() after $("body") but when I use click or hover the string appeared repeatedly

Can anyone give me some example / opinion so that .append() jQuery function will work perfect for me?

I'm just trying to add some string or HTML tag inside div #OL_Icon_63 using jQuery .append(), here's the example of code :

$(document).ready(function() {
    $('#OL_Icon_63').append('HELLO WORLD');
});

But that's code won't work for me, I try to change the code like this :

$(document).ready(function() {
    $("body").click(function() {
       $('#OL_Icon_63').append('HELLO WORLD');
    });
});

And

$(document).ready(function() {
    $("body").hover(function() {
       $('#OL_Icon_63').append('HELLO WORLD');
    });
});

Two kinds of code work for me where I use .hover() and .click() after body tag, but i basically can not use the function like .append() or anything else without adding .hover() or .click() after $("body") but when I use click or hover the string appeared repeatedly

Can anyone give me some example / opinion so that .append() jQuery function will work perfect for me?

Share Improve this question edited Aug 8, 2012 at 18:56 Jhonny Jr. asked Aug 8, 2012 at 18:45 Jhonny Jr.Jhonny Jr. 3063 gold badges4 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

A more general solution is using the fact javascript functions are objects, once the function clickAction is called it will set itself to a blank function after appending the text

EDIT This does not work in jQuery due to the way it handles events, however it does contain a method called one()

$(document).ready(function() {
  var clickAction = function(){
     $('div').append("Hello World");
  } 
    $("div").one("click",clickAction);
});​

Use text instead:

$('#OL_Icon_63').text("hello world")

You are using append incorrectly. Append is for adding elements to the innerHTML of an element. text is for text, and html is for HTML

This will also replace all text in the element, so there will be no multiples.

why don't you try without $(document).ready this works for me:

(function(){
    $('#OL_Icon_63').append('HELLO WORLD');
})();

Working example: http://jsfiddle/tgzKQ/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745166313a4614651.html

相关推荐

  • javascript - Add some string inside div tag using jQuery - Stack Overflow

    I'm just trying to add some string or HTML tag inside div #OL_Icon_63 using jQuery .append(), here

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信