我想将占位符属性添加到这个DIV中的输入,但是它没有唯一的属性,我可以用它来定位它

I want to add the placeholder attribute to the input within this DIV but it does not have a unique property that I could locate it with

本文关键字:属性 唯一 我可以 定位 添加 DIV 输入 占位符      更新时间:2023-09-26

我不能直接编辑HTML,因为Squarespace不允许,只能使用JS和Jquery。

$("#field city :input").attr('placeholder', 'City');

我试着使用这个,但它不工作

<div class="field city">
                <label class="caption"><input class="field-element field-control" name="" x-autocompletetype="" type="text" spellcheck="false" data title="">
                </label>
              </div>

Thanks in advance

您的选择器对所提供的html不正确。

如此:

$(".field.city .field-element").attr('placeholder', 'City')

小提琴

检查这个js文件…

<div class="field city">
  <label class="caption"><input class="field-element field-control" name="" x-autocompletetype="" type="text" spellcheck="false" data title="">
  </label>
</div>
<script>
    document.getElementsByClassName('field-element')[0].placeholder='city'
</script>