最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Saving a JQuery DOM element as a variable - Stack Overflow

programmeradmin1浏览0评论

I am trying to store a DOM element into a variable by using this code:

var save_status = $(this).siblings(".save-status")[0];
save_status.append("<img src="images/loading-small.gif" width=30 />");

I get the following error though:

Uncaught SyntaxError: Unexpected identifier

Any idea why I might be getting this error? Thanks!

I am trying to store a DOM element into a variable by using this code:

var save_status = $(this).siblings(".save-status")[0];
save_status.append("<img src="images/loading-small.gif" width=30 />");

I get the following error though:

Uncaught SyntaxError: Unexpected identifier

Any idea why I might be getting this error? Thanks!

Share Improve this question asked Jul 19, 2011 at 0:26 egidraegidra 3472 gold badges9 silver badges18 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

You're getting the error because your quotes are not escaped (or literal).

To answer your other question (per the title and example), you can save a jQuery object, or a DOM element, but they're not the same. For example:

var x = $(selector); // returns jQuery object
var y = $(selector)[0]; // returns DOM element

x.append('stuff'); // use jQuery functions directly
$(y).append('stuff'); // pass the DOM element to jQuery first

Replace

save_status.append("<img src="images/loading-small.gif" width=30 />");

with

save_status.append('<img src="images/loading-small.gif" width=30 />');

The problem is in your quotations.

I see several things. Try this example:

<style>
#mydiv{
    display: block;
    width: 100px;
    height: 100px;
    background-color: red;
}
</style>
<script src="../assets/js/jquery-1.6.min.js" type="text/javascript"></script>
<script>
$(function(){
    $("#mydiv").click(function(){
        var save_status = $(this).siblings(".save-status").next();
        save_status.append('<img src="images/loading-small.gif" width=30 />');
        alert(save_status);
    });
});
</script>
<body>
    <div id="mydiv">
        <div></div>
        <div></div>
        <div class="save-status"></div>
    </div>
</body>

The differences are the quotes and use ".next()"

发布评论

评论列表(0)

  1. 暂无评论