Javascript jQuery相当于document.getElementById().size
为了查找文本框元素的大小,我正在研究与以下HTML DOM方法等效的jQuery:Javascript jQuery相当于document.getElementById().size,javascript,jquery,html,Javascript,Jquery,Html,为了查找文本框元素的大小,我正在研究与以下HTML DOM方法等效的jQuery: <!DOCTYPE html> <html> <body> Name: <input type="text" id="myText"> <p>Click the button to set the width of the text field.</p> <button onclick="myFunction()">Try
<!DOCTYPE html>
<html>
<body>
Name: <input type="text" id="myText">
<p>Click the button to set the width of the text field.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert(document.getElementById("myText").size);
document.getElementById("myText").size = "50";
alert(document.getElementById("myText").size);
}
</script>
</body>
</html>
改为
alert($("myText").val().length); //shows length of the value inside textbox.
alert($("myText").length); // shows 1 in the output and its expected because its jQuery's way of showing number of elements not its size.
alert($("myText").size()); // shows 1 in the output. This is equivalent to the length property. using size() is jQuery's preffered way as using length is deprecated.
我尝试了jquery,但无法找到我要查找的内容。相关的stackoverflow问题讨论如何在文本框中查找值的长度,但我正在查找文本框的大小。我的用例需要找出UI屏幕上文本框的大小,以确定工具提示框和其他弹出窗口的位置。使用$myText.propsize:
函数myFunction{
log$myText.propsize;
$myText.propsize,50;
log$myText.propsize;
}
姓名:
单击按钮以设置文本字段的宽度
试试看你可以拿走你的旧DOM。如果您想使用CSS3选择器,这会更好
document.querySelector("#css3selector").size
它的工作原理与旧DOM类似我非常怀疑您的HTML.alertdocument.getElementByIdmyText.size中是否有myText标记;只是因为你有jquery,并不意味着一切都必须使用jquery方法;您正在通过id进行搜索,因此请使用CSS id选择器:myText,然后读取和。浏览API真的不需要那么长时间,花这么短的时间是值得的。@AndrewLi真的!!那么下面的代码在我的HTML中是什么意思@Vishal,这是一个id,do$myText
document.querySelector("#css3selector").size