gpt4 book ai didi

javascript - 自定义属性不起作用

转载 作者:行者123 更新时间:2023-12-01 02:27:56 24 4
gpt4 key购买 nike

我创建了一个自定义属性来为每个链接添加标题。

<a href="#" nameOf="The Flower" id="ex1"></a>
<a href="#" nameOf="The Tree" id="ex2"></a>

这是 jQuery 代码

$('#ex1,#ex2').append('<span class="title">'+$(this).attr("nameOf")+'</span>');

但链接显示为未定义。我该如何解决这个问题。

最佳答案

迭代元素标签并附加到它

$('a').each(function(i, v) {
$(this).append('<span class="title">' + $(this).attr("nameOf") + '</span>');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" nameOf="The Flower" id="ex1"></a>
<a href="#" nameOf="The Tree" id="ex2"></a>

关于javascript - 自定义属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48594039/

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