你不需要也不应该document.write()
在这种情况下使用。
你可以这样做:
<script type="text/javascript">
function update_bs_3(){
var el = document.getElementById('bs3');
if (el.innerHTML.trim() == '333 Johnson') {
el.innerHTML = '<div style="float:left; padding-left:10px; padding-top:10px;"><img src="/checkmark.png" title="Project Entered"></div>';
}
}
</script>
然后将事件处理程序放到您的 body 元素中:
<body onload="update_bs3();">
这是小提琴http://jsfiddle.net/DxjGv/1/
编辑:这是多个 div 的版本:
HTML:
<div id="bs1" class="survey_dashboard_item" onclick="window.open('/building-specifications?bldgaddress=111 Steve','_self');" style="font-size:40px; border:1px solid #bbb;">
111 Steve</div>
<div id="bs2" class="survey_dashboard_item" onclick="window.open('/building-specifications?bldgaddress=222 Frankie','_self');" style="font-size:40px; border:1px solid #bbb;">
222 Frankie</div>
<div id="bs3" class="survey_dashboard_item" onclick="window.open('/building-specifications?bldgaddress=333 Johnson','_self');" style="font-size:40px; border:1px solid #bbb;">
333 Johnson</div>
JS:
function update_bs_divs(){
var divs = {
'bs1': {
'match': '111 Steve',
'replacement': '<div>Steve replaced</div>'
},
'bs2': {
'match': '222 George',
'replacement': '<div>George replaced</div>'
},
'bs3': {
'match': '333 Johnson',
'replacement': '<div>Johnson replaced</div>'
}
};
for (var id in divs) update_div(id, divs[id].match, divs[id].replacement);
};
function update_div(id, match, replacement){
var el = document.getElementById(id);
if (!el || (el.innerHTML.trim() != match)) return;
else el.innerHTML = replacement;
}
window.onload = update_bs_divs;
将此 JS 代码保存在一个文件中,并将其包含在您的 HTML 头部部分中。
小提琴在这里 - http://jsfiddle.net/DxjGv/2/