选择<a>标记,其中<输入>与jQuery在一起

Select <a> tag where <input> is inside with jQuery

本文关键字:gt lt jQuery 在一起 输入 标记 选择 其中      更新时间:2023-09-26

我需要将style="text-decoration: none;"应用于包含<input>标记的所有<a>标记。由于我不能用CSS来做这件事,我想jQuery可以帮助我找到这个案例的位置,并应用样式。

示例:

<a href="http://www.google.com"><input type="button" value="Click here to go to Google!" /></a>

我知道.css('text-decoration', 'none')应用CSS,但我该如何找到标签

您可以使用:has选择器:

$("a:has(input)").css("text-decoration", "none");

.has方法:

$("a").has("input").css("text-decoration", "none");

演示

如果你只想要那些直接父母,你可以使用

$("input").parent("a");

如果你想要最近的祖先,你可以使用

$("input").closest("a");

如果你想要所有的祖先,你可以使用:

$("input").parents("a");

实现这一点(至少)有两种方法:

$('a').has('input').css('text-decoration', 'none');
$('a:has(input)').css('text-decoration', 'none');
$('a:has(input)')

$('input').parents('a')