gpt4 book ai didi

jquery - 属性分配不起作用

转载 作者:行者123 更新时间:2023-12-01 06:46:34 27 4
gpt4 key购买 nike

我正在尝试模仿占位符行为,因此我尝试在页面加载时保存原始值。使用此代码。我在这里是因为它不起作用。该值为空。

$().ready(function() {
$("input").prop("placeHolder", $(this).val());
/*
also tried:
$("input").prop("placeHolder", $(this).attr("value"));
*/
}

最佳答案

试试这个:

$(document).ready(function(){
$("input").prop("placeholder", function(){
return $(this).val()
});
});

使用 $().ready() 我猜你指的是 $(document).ready() 方法。

在您的情况下,$(this) 没有引用您的输入。它正在从该句子所在的位置获取父级。在本例中为 $()

你可以按照我上面写的做,或者这也可以:

$(document).ready(function(){
$("#input_id").prop("placeholder", $("#input_id").val());
});

这适用于一个输入,如果您想要另一种方式使其适用于所有输入,这也适用:

$(document).ready(function(){
$("input").each(function(){
$(this).prop("placeholder", $(this).val());
});
});

请注意,在这种情况下 $(this) 确实引用了正在迭代的输入。

关于jquery - 属性分配不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25749322/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com