Ash*_*mar 6 html javascript jquery
我正在做一些程序,当它检测到文本框中的输入更改时,它需要做一些事情。但这对我不起作用。所以我更改我开始在控制台中给出值以验证它每当它检测到输入更改时它总是返回undefined。如何获取用户输入的文本大小(以像素为单位)?
<!doctype html>
<html>
<head>
<style>
input[type=text] {
height: 20px;
width: 100px;
}
</style>
<script src="jquery.js"></script>
</head>
<body>
<input type="text" id="func"></input>
</body>
<script>
$("#func").change(function () {
console.log($("func").scrollWidth);
});
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
您的代码中有两个错误。首先,选择func没有散列的元素。如果在 jQuery 中选择 id,则需要#在 id 名称前使用 a (请参阅参考资料)。所以,你需要选择这样的:$('#func')。
其次,您需要 DOM 元素$('#func')而不是其 jQuery 包装器来访问属性scrollWidth。您可以使用$('#func')[0].scrollWidth或来做到这一点$('#func').get().scrollWidth。
但是,我建议您使用jQuerywidth函数来获取输入字段的宽度:$('#func').width().
| 归档时间: |
|
| 查看次数: |
12533 次 |
| 最近记录: |