-2

我在我的 javascript 中有这个,但是其中的 javascriptdocument.getElementById不起作用。

document.getElementById('addcategory1').innerHTML += "<input type='text' class='input-short' name='newcategory[1]' value='New Category 2' onfocus='javascript: if(this.value == 'New Category 2'){ this.value = ''; }' onblur='javascript: if(this.value==''){this.value='New Category 2';}'/><br/><br/>";

此 javascript 不起作用:

onfocus='javascript: if(this.value == 'New Category 2'){ this.value = ''; }'

onblur='javascript: if(this.value==''){this.value='New Category 2';}'
4

2 回答 2

1

正确转义引号。使用双引号将单引号括起来。

onfocus="javascript: if(this.value == 'New Category 2'){ this.value = ''; }" onblur="javascript: if(this.value==''){this.value='New Category 2';}"
于 2013-10-20T16:02:17.817 回答
0

我相信这里有引用问题:

onfocus='javascript: if(this.value == 'New Category 2'){ this.value = ''; }' onblur='javascript: if(this.value==''){this.value='New Category 2';}'

您在单引号脚本中嵌套了单引号。

尝试将其替换为:

onfocus='if(this.value == "New Category 2'){this.value = ""; }' onblur='if(this.value==""){this.value="New Category 2";}'

您更大的innerHTML代码将是:

document.getElementById('addcategory1').innerHTML += "<input type='text' class='input-short' name='newcategory[1]' value='New Category 2' onfocus='if(this.value == \"New Category 2\"){ this.value = \"\"; }' onblur='if(this.value==\"\"){this.value=\"New Category 2\";}'/><br/><br/>";
于 2013-10-20T16:02:04.930 回答