嗨,我很久以前就遇到过这个问题,我浪费了很多时间来找出问题所在。让我讲一下情节。假设我有一个 html 页面,其中我已经在页面中的元素很少,并且我正在使用 ajax 调用动态添加一些元素。在向页面添加新项目时,我遇到了这样的情况,例如我需要为其中一个元素添加 onClick 函数调用以及作为参数的对象。但是当我在 jquery 中添加该函数时,函数被调用但我在函数中得到的对象与我传入的对象不同。对象被转换为字符串而不是对象。
<html>
<body>
.
.
.
... Some html code....
.
.
.
.
<div onclick="function1();"> </div>
.
.
<div id="dynamicElement"> </div>
.
.
</body>
<script type="text/javascript">
function function1(){
.
.
$.ajax(){
success(obj):{
$("#dynamicElement").empty().append('<input id="iButton" type="button" onclick="function2('+obj+')">')
}
}
}
</script>
<script type="text/javascript">
function2(obj){
//Here When I try to manipulate with this object. I was actually not abt to do.
// this is because of the object u add in dynamic elements will get converted to string instead of ibject.
}
</script>
</html>