2017-04-11 41 views
0

HTML如何找到一个JS对象的属性上

<div class="outerDiv"> 
    <div class="innerDiv"> 

    </div> 
    <button id="appendObject">Click Me</button> 
    </div> 
</div> 

的JavaScript

var newArray = [ 
{ 
    id: 0, 
    otherProperty: "this is a prop" 
}, 
{ 
    id: 1, 
    otherProperty: "this is a different prop" 
    } 
]; 
$("#appendObject").click(function() { 
    $(".innerDiv").append("<div class='this-object'>" + newArray[0].otherProperty) + "</div>"; 
}) 

$("body").on("click", ".this-object", function() { 
    alert(this.id); 
}) 

我想一个对象的属性添加到HTML DIV点击时。然后我希望用户能够单击该HTML div并能够更改该对象的属性。无论如何,我可以做到这一点?谢谢!

https://jsfiddle.net/of5x5egr/

+0

你是什么意思,改变属性? Css,高度宽度...稍微清晰一点。 – Cam

+0

我想弄清楚哪个JavaScript对象被点击,所以我可以让用户改变对象的特定属性。例如:newArray中的第一个对象 - 我想让用户通过在输入字段中输入一个值来更改otherProperty的值 –

回答

0

你有一个语法错误的位置:

$(".innerDiv").append("<div class='this-object'>" + newArray[0].otherProperty) + "</div>"; 

应该

$(".innerDiv").append("<div class='this-object'>" + newArray[0].otherProperty + "</div>"); 

https://jsfiddle.net/of5x5egr/2/

相关问题