[英]Accessing html tag attributes
I'm trying to test a very basic javascript function that will only remove an attribute from an input. 我正在尝试测试一个非常基本的javascript函数,该函数只会从输入中删除属性。 In this case, i want the placeholder attribute to be removed, and this is not working:
在这种情况下,我希望删除占位符属性,但这不起作用:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function buttonClicked() {
document.getElementById("textBox").removeAttribute('placeholder');
}
</script>
</head>
<body>
<div id="commentBox">
<input type="textarea" rows="4" cols="50">
</div>
<div id="textBox">
<input type="text" placeholder="Write">
</div>
<div id="shoutButton">
<input type="button" value="Shout!" onclick="buttonClicked();">
</div>
</body>
</html>
In the provided code "textBox" is the id of the div
element which wraps your input
. 在提供的代码中,“ textBox”是包装
input
的div
元素的ID。 You need to get the input instead. 您需要获取输入。 Try setting the id of the input as well:
尝试同时设置输入的ID:
<div id="textBox">
<input id="input" type="text" placeholder="Write">
</div>
Then update your buttonClicked function to use the input's id: 然后更新您的buttonClicked函数以使用输入的ID:
function buttonClicked() {
document.getElementById("input").removeAttribute('placeholder');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.