[英]Can you use jQuery's $(this) in a one-line code to modify elements?
I'm working on a placeholder function in jquery. 我正在jquery中处理占位符函数。 Right now, I just want the form element to change its value to whatever its placeholder is. 现在,我只想让表单元素将其值更改为占位符。 I tried the following code: 我尝试了以下代码:
$('input:text').val($(this).attr('placeholder'));
But it doesn't work. 但它不起作用。 After testing it a little, I realized the problem is with using $(this)
in that context. 经过一段时间的测试后,我意识到问题在于在这种情况下使用$(this)
。 How can I change this so that it will loop through all form elements and change their value to their placeholder attribute? 如何更改它以便循环遍历所有表单元素并将其值更改为其占位符属性?
Most of these setter jQuery functions provide a way to specify a function whose return value will be used. 这些setter jQuery函数中的大多数提供了一种指定将使用其返回值的函数的方法。 This is a way to make use of $(this)
. 这是一种利用$(this)
。
$('input:text').val(function () {
return $(this).attr('placeholder');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.