【发布时间】:2010-09-03 03:42:10
【问题描述】:
同学们,
我写了一些代码来保存网页的当前状态。当有一个文本框通过输入更改值时,该更改未在 html 代码中注册。即使我打电话:
object.value = 'x';
is 不会改变值。当我使用时:
object.setAttribute('value','x');
然后在 html 代码中注册更改。
循环遍历设置 onchange 属性的文本框不是解决方案,因为我有一些特殊的文本框,它们已经设置了 onchange 属性。
感谢您的帮助
function SaveHTML()
{
removeThis(get('Source'));
var newdiv = document.createElement('div');
newdiv.setAttribute('id','Source');
newdiv.style.position= 'absolute';
newdiv.style.top= (tempY-200)+'px';
newdiv.style.left= (tempX+30)+'px';
newdiv.style.backgroundColor = 'white';
var html=encodeURI(document.getElementsByTagName('body')[0].innerHTML);
newdiv.innerHTML='HTML:<br><textarea rows="20" cols="60">'+html+'</textarea><br><input type=button value=close onclick=removeParent(this)>';
document.body.appendChild(newdiv);
}
function LoadHTML()
{
removeThis(get('Source'));
var newdiv = document.createElement('div');
newdiv.setAttribute('id','Source');
newdiv.style.position= 'absolute';
newdiv.style.top= (tempY-200)+'px';
newdiv.style.left= (tempX+30)+'px';
newdiv.style.backgroundColor = 'white';
newdiv.innerHTML='HTML:<br><textarea id=code rows="20" cols="60"></textarea><br><input type=button value=cancel onclick=removeParent(this)> <input type=button value=load onclick=document.getElementsByTagName(\'body\')[0].innerHTML=decodeURI(get(\'code\').value)>';
document.body.appendChild(newdiv);
}
function get(Element)
{
return document.getElementById(Element);
}
function removeThis(obj)
{
try{
obj.parentNode.removeChild(obj);
}
catch (e) {
return;
}
}
function removeParent(obj)
{
obj.parentNode.parentNode.removeChild(obj.parentNode);
}
【问题讨论】:
-
您的问题是为什么
setAttribute有效而o.value = ...无效?
标签: javascript html dom