我正在尝试获取 placeholder
属性值并使用 placeholder
值作为 的标签执行
值,但它不起作用。fadeIn
对于
HTML:
<html>
<body>
<form>
<input type="text" id="name" name="name" placeholder="First Name" />
<label for="First Name">First Name </label>
</form>
</body>
</html>
CSS:
input+label { display: none; }
脚本
$(document).ready(function() {
$('input[type="text"]').click(function() {
var sd = $(this).attr('placeholder');
$('label[for^=sd]').fadeIn();
});
});
最佳答案
您选择的是文字字符串“sd”,而不是变量的值。试试这个:
var sd = $(this).attr('placeholder');
$('label[for^="' + sd + '"]').fadeIn();
关于jquery - 如何使用 jquery 获取占位符属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8416223/