我有一个示例代码:
<span id="app_name_1"></span><a onclick="addSelectApplication('name 1');" href="#">Add name 1</a>
<span id="app_name_2"></span><a onclick="addSelectApplication('name 2');" href="#">Add name 2</a>
<span id="app_name_3"></span><a onclick="addSelectApplication('name 3');" href="#">Add name 3</a>
和 javascript
<script>
function addSelectApplication(title) {
for(var i=0; i<3; i++) {
parent.document.getElementById('app_name_'+i).innerHTML = title;
}
}
</script>
当我点击Add name 1
时,结果是
<span id="app_name_1">name 1</span>
<span id="app_name_2">name 1</span>
<span id="app_name_3">name 1</span>
如何修复结果是:
点击时Add name 1
<span id="app_name_1">name 1</span>
<span id="app_name_2"></span>
<span id="app_name_3"></span>
点击时Add name 2
<span id="app_name_1"></span>
<span id="app_name_2">name 2</span>
<span id="app_name_3"></span>