任何人都可以帮忙清除文本框中的内容。我尝试使用 javascript 在单击图像时清除文本框中的内容
<input name="newKey" id="newKey" type="text" value="helo" size="38" maxlength="45"/>
<span class="btnClr" id="clear" onclick=clearThis("newKey"></span>
function clearThis(target){
target.value= "";
}
最佳答案
您正在清除名为“target”的字符串:)
您要清除的是 DOM 元素本身:
function clearThis(target) {
target = document.getElementById(target);
target.value = "";
}
此外,您的 onclick
属性需要引用以避免歧义:
onclick='clearThis("search")'
Here is a working fiddle
<小时/>另一方面,请考虑使用不那么引人注目的 JavaScript。更容易维护和开发。调试属性字符串内的代码可能是一场真正的噩梦,并且可能会产生可移植性问题。
类似于:
var clear = document.getElementById("clear");
var search = document.getElementById("search");
function clearThis(element) {
element.value = "";
}
clear.onclick = function(){
clearThis(search);
}
并且 HTML 中没有 JavaScript
关于javascript - 单击图像时清除文本框中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17941255/