是否有一种特殊的方法来修改属性onclick?
var btn = document.getElementById("deletebutton" + j);
btn.id='deletebutton'+mreplace;
btn.onclick = 'deleteDiv('+mreplace+')';
id会改变,但onclick保持不变。
我正在尝试切换一些Id和数据,以便在从页面删除元素后重建页面。在这种情况下,我有7个div,当我擦除说第3个,我想修改其他div,以便他们的文本更新---这我已经完成---现在问题只是更新的onclick功能删除按钮,因此单击时将删除正确的div。
调用完整的函数。
function deleteDiv(id) {
var div = document.getElementById("idBox" + id);
div.parentNode.removeChild(div);
// If countbox > id
if (countBox() > id) {
var j = countBox();
while (j > id) {
var div = document.getElementById("idBox" + j);
var mreplace = j-1;
var replace = j+1;
var html = div.innerHTML;
var curInnerHTML = html.replace("Question "+replace,"Question "+j);
div.innerHTML = curInnerHTML;
div.id='idBox'+mreplace;
var btn = document.getElementById("deletebutton" + j);
btn.id='deletebutton'+mreplace;
console.log(mreplace);
btn.onclick = 'deleteDiv('+mreplace+');';
j = j - 1;
}
}
}
这里是生成的一个可以删除的div的html
<div class="Cbox" id="idBox1">
<div class="leftpart">Question 2:</div><div class="rightpart"><span class="target" data-field="3" contenteditable="true">Questijjon # 2</span>
<br>
<select class="super" name="3" onchange="getCombo(this)">
<option value="MC" name="0" data-field="0">
Multiple Choice</option>
<option value="TF">
True/False</option>
<option selected="true" value="INT">
Integer</option>
</select>High: <span class="intHigh" data-field="3" contenteditable="true">50</span> Low: <span class="intLow" data-field="3" contenteditable="true">15</span>
<form action="" method="post">
<input class="deletebutton" id="deletebutton1" value="-" name="delete" onclick="deleteDiv(1);" 1'="">
</form></div>
</div>
答案 0 :(得分:0)
btn.onclick = function() {
deleteDiv(mreplace);
};