Accessing a javascript object with jQuery
After creating an object with JavaScript I am looking to access it with jQuery to manipulate it. Here is some code to make things clearer:
ObjectFunction = function(content) {
this.content = content
}
ObjectFunction.prototype = {
showobject : { return this.appendTo(document.body)}
}
obj = New ObjectFunction()
What I would like to do is as follows, but the syntax is wrong:
$(obj).css{(background:'red')}
This doesn't work. If I create a function in prototype, which would look something like this, it works:
ObjectFunction.prototype = {
showobject : { return this.appendTo(document.body)},
objectmodify: { return this.css{(background:'red')}}
}
// then call something like
obj.objectmodify()
Is there any way to avoid this ugly code for a direct jQuery function 开发者_开发技巧applied on the object?
$(obj).css{(background:'red')}
The syntax of this is wrong, you have the parenthesis and squigly's the wrong way round - it should be
$(obj).css({background:'red'});
I figured out the answer. as always it was simpler than i thought.
the dom element created is referenced as a property of the javascript object. which can be accessed in this case ( see the code above ) by obj.content so in the is case $(obj.content).css({background:'red'}). works :) I hope this helps someone out there :)
I think you have to write:
$(obj).css("background","red");
or
$(obj).attr('style', 'background:red');
精彩评论