简体   繁体   中英

jQuery objects don't work

When I store a jQuery object in a variable, like this:

var $myObject = $("div#comments");

...I can't use the object $myObject!

This is what I'm doing to change the html of div#comments:

$myObject.html(data);

It does nothing. I already tried this way too, this time to select an element inside div#comments:

$("div.comment", $myObject);

It doesn't work.

I just want to be able to save an element in a variable and then use it !

Note: some people don't put $ before the variable name, like this: myObject.

Are you calling it after the document is loaded?

     // This will ensure that the code doesn't run until
     //    the document has loaded
$(function() {
    var $myObject = $("div#comments");
});

(This is a shortcut for jQuery's .ready() method.)

As long as the document is loaded, and you have a <div> with the ID comments on the page when it loads, it should work.

Also remember that there can only be one element on the page with any given ID. Because of this, it is actually a little better (quicker) to do $("#comments"); instead of $("div#comments"); .

You've only provided snippits of your code, so it is impossible to tell for sure, but the odds are that you are running the code in a <script> element that appears before the <div> element and don't do anything (such as use the ready event) to delay the execution of the code until the div exists.

The result is that you get a jQuery object which found no elements. Move the script element so it is after the div. Just before the end tag for the body is a good place.

The syntax is perfectly valid and should work. Are you dynamically appending the comments div? You should alert( $myObject.length ) to see if it's 0 or 1, if its 0 that means it's never picked up.

You may need to bind the var statement until after dom ready, window load, or your ajax callback.

Well, that syntax is perfectly fine so something else is going on. Can you show your markup? And what do you get if you add an alert($myObject.length)? And one last thing to check... are you running this inside an on-ready handler?

Ok, thanks everyone for that. I got the solution.

I thought about the order the things were loaded in the DOM and found the solution. The problem (with the markup) was:

<div id="comments">    
    <script type="text/javascript">
    loadComments(params);
    </script>
</div>

The code above was written by PHP! So it executed the function as soon as the browser read the code. I already tried to put the script on the end of the page, after the function was called . The funcion was not defined yet.

So, the funcion loadComments should be executed after the div was ready AND after the function was defined.

I wrapped the code between the tags with a .ready(), like this:

<script type="text/javascript">
$(function() {
    loadComments(params);
});
</script>

It was a distraction. Sorry everyone! Thanks a lot.

If you have the same problem and you didn't understand what I did, ask me. XD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM