In an earlier question我制定了如何将object
作为properties
存储。 现在我要当object
通过事件传递给访问这些properties
但无法得到它的工作:jQuery - 从传递给事件的另一个对象中访问对象属性
<script language="javascript">
$(document).ready(function() {
//create a TestObject
function TestObject() {
this.testProperty = "green";
}
//and an instance of it
var testObject = new TestObject();
//attach this instance to the div as a property
var test;
test = $('#test');//the div
jQuery.data(test, "obj", testObject);
//prove it worked and the TestObject is assigned
alert(jQuery.data(test, "obj").testProperty);//works
$('#test').click(TestClick);
//test.click(TestClick); doesn't work either
function TestClick() {
alert($(this).attr("id"));//displays "test" - works
alert(jQuery.data($(this), "obj").testProperty);
//testProperty is null or not an object??
//clearly TestObject is no longer attached to the div, why?
//Or have I attached it the wrong way?
//alert(jQuery.data(this, "obj").testProperty); doesn't work either
};
});
</script>
<body>
<form id="form1" runat="server">
<div id="test">Here is a test div</div>
</form>
</body>
这也行不通,在'jQuery.data()'你存储的DOM元素的引用仍然不一样...在正常情况下,你不应该使用'jQuery .Data()',而是'.data()'。 – 2010-03-01 12:54:57