0
呼籲動態添加的DOM元素的onclick javascript函數,您好我面臨這個問題很長回來了,我浪費了很多時間來弄清楚什麼是錯的。 讓我告訴情景。假設我有一個html頁面,我有幾個已經在頁面中的元素,並且我使用ajax調用動態添加了幾個元素。在向頁面添加新項目時,我得到了像我需要添加onClick函數調用一個元素以及一個對象作爲參數的場景。 但是,當我再補充一點jQuery中的函數獲取調用,但我在功能上得到的對象是不一樣的,因爲我傳遞的對象。對象是越來越轉換爲字符串,而不是目的。用ajax
<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>
這是jQuery的開始絆倒。你已經到了像jQuery這樣的庫可以爲你做的邊緣。 _Nice_解決方案位於文檔片段符號庫的領域。即。你想要內聯指定一個onclick處理程序,但你不想要混亂的字符串轉換。 – Halcyon