这边有一个疑惑,是关于javascript 属性监听得问题?麻烦大佬帮忙看看

<msg id="msg1" message="消息" >
<div id="div1" ></div>
</msg>
<input type="button" onclick="cli()" /> 
<script>
function cli() {
msg1.message = "消息2";
}
</script>

自定义标签<msg> 属性为123

当点击的时候 
div的innerHTML 也自动改变

如何实现
我是想让 
msg1.message = "消息2";
div1.innerHTML = "msg1.message";
是把
msg1.message与div1.innerHTML 关联起来
只改第一句,第二句自动改变

精慕HU
浏览 328回答 2
2回答

沧海一幻觉

function cli(){div1.innerHTML=msg1.message='消息2';}

慕尼黑的夜晚无繁华

首先,本人未找到js属性绑定的方法,只能使用setTimeout;下面代码已经过IE和FF的测试:<msg id="msg1" message="消息"><div id="div1" ></div></msg><input type="button" onclick="cli()" value="改变msg"/><script>function cli() {document.getElementById('msg1').setAttribute('message', '改变了');//改变的只是message属性的值}var lastValue = '消息';function tongbu() {nowValue = document.getElementById('msg1').getAttribute('message');if(lastValue != nowValue) {document.getElementById('div1').innerHTML = nowValue;//当message属性值改变,则改变div1的内容为新值lastValue = nowValue;}timerID = setTimeout("tongbu()", 1000);//每隔一秒执行一次}window.onload = tongbu;//页面载入完成后立即执行</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

CSS3
JavaScript