如何使用jquery选择元素值?
问题
我们如何应用 jQuery 选择器来根据 HTML 值返回元素?
我知道一定有很多方法可以做到这一点,但我想知道是否有任何简短而好的方法可以做到这一点。
想象一下这个 HTML:
<element>valuable</element>
我想要一个像这样的 jQuery:
$('element[value="valuable"]').doSomething();
糟糕的解决方案
我现在解决这个问题的方法是将 HTML 更改为:
<element value="valuable">valuable</element>
当然,我也可以按照相同的逻辑并在 jQuery 上执行此操作来解决这个问题:
$('element').each(function(i){$(this).attr("value",$(this).html());});
我不知道不知道哪个最差!
Question
How can we apply a jQuery selector to return elements based on their HTML value?
I know there must be many ways to do this, but I wonder if there's any short and good way to do it.
Picture this HTML:
<element>valuable</element>
I'd want a jQuery like this:
$('element[value="valuable"]').doSomething();
Bad Solutions
The way I've solved this for now is by changing the HTML into this:
<element value="valuable">valuable</element>
Sure I could also solve this following the same logic and doing it on jQuery:
$('element').each(function(i){$(this).attr("value",$(this).html());});
I don't know which one is worst!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后找到了...在做我的事情之前我太急了最后的搜索方法:提问。我通常输入标题,查看已经发布的建议,尝试修改标题,再看一遍...这次我已经写了答案,忘记看那里,当我按“发布”时,我记得去尝试一下。
所以我一直在寻找的答案是:
Finally found it... I rushed too much ahead of myself before doing my last search method: asking a question. I usually type the title, look at the suggestions already posted, try modifying the title, look again... This time I already wrote the answer and forgot to look there, and when I pressed "post" I remembered to go and try it.
So the answer I was looking for would be: