jquery not working to change inner html?

后端 未结 4 515
予麋鹿
予麋鹿 2020-12-03 18:03

hey people I\'m new at jquery and i have been trying to figure this out for like the past couple of hours... what the hell am I doing wrong here? All I want to do is change

相关标签:
4条回答
  • 2020-12-03 18:31

    Since left_menu_bar isn't an element, you can't select it that way.

    If it's the ID of an element, try:

    $("#left_menu_bar").html("You clicked me!");
    

    If it's the class of an element, try:

    $(".left_menu_bar").html("You clicked me!");
    
    0 讨论(0)
  • 2020-12-03 18:38

    Do it like this:

    // referencing by id
    $("#left_menu_bar").html("You clicked me!");
    // referencing by class name ( will apply to all div's with this class )
    $(".left_menu_bar").html("You clicked me!");
    

    http://api.jquery.com/html/

    0 讨论(0)
  • 2020-12-03 18:54

    You're attempting to assign some text to a property named innerHTML on an object that doesn't have a property named innerHTML.

    The $ function in jQuery doesn't return a DOM element. It returns a jQuery object which provides the ability to execute all the wonderful jQuery functions on set particular DOM elements.

    One of the functions the jQuery object exposes is .get() which will return one of the elements it contains.

    So in your case, you could do:

    // 0 means it will return the first element in the jQuery object
    $("left_menu_bar").get(0).innerHTML = "You clicked me!";
    

    Or as @helmus stated in his answer, you could use the .html() function that jQuery provides to set the content which actually uses innerHTML on the inside.

    0 讨论(0)
  • 2020-12-03 18:56

    jQuery always returns a jQuery collection--not a DOM element. In order to access DOM properties directly, you'll need to retrieve an element from the collection:

    $(".left_menu_bar").first().innerHTML = "You clicked me!";
    

    This isn't very convenient, however, and helmus's answer describes the way that things are usually done.

    0 讨论(0)
提交回复
热议问题