研究员,
我写了一些代码来保存网页的当前状态。如果有一个文本框,我通过输入更改了值,则更改未在html代码中注册。即使我打电话:
object.value = 'x';
不会更改该值。当我使用时:
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);
}
答案 0 :(得分:0)
您可以在保存HTML时循环浏览每个输入字段,然后执行
object.setAttribute("value", object.value)
您还可以使用其中一个大框架的事件处理函数,它们可以附加事件而不会覆盖旧事件。
答案 1 :(得分:0)
setAttribute和getAttribute,因为它们不是标准,使用.value应该可行。但另一种方法是使用jQuery的attr方法。
$('#myID').attr('id', 'newID');
$('#myID').attr('value', 'newValue');
此外,你应该使用这样的正确引号:
newdiv.innerHTML='HTML:<br><textarea rows="20" cols="60">'+html+'</textarea><br><input type="button" value="close" onclick="removeParent(this)">';
和
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)">';
要为特定事物设置onchange,请尝试使用jQuery选择器:
$('#myID').onchange(function(){/* your stuff here */});
答案 2 :(得分:0)
在SaveHTML调用中,我运行此函数。像魅力一样工作!
function TouchInputs()
{
var everything = document.getElementsByTagName('input');
var everythinglength = everything.length;
for(var i = 0;i<everythinglength;i++)
{
try{
everything[i].setAttribute('value',everything[i].value);
}
catch(e){
alert(e.message);
}
}
}