选择<a> tag where <input/> is inside with jQuery</a>

时间:2012-02-06 16:44:09

标签: javascript jquery css

我需要将style="text-decoration: none;"应用于包含<a>标记的所有<input>代码。由于我不能用CSS做到这一点,我认为jQuery可以帮助我找到这种情况的位置,并应用这种风格。

示例:

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

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

4 个答案:

答案 0 :(得分:5)

您可以使用:has选择器:

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

.has方法:

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

Demo.

答案 1 :(得分:4)

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

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

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

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

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

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

答案 2 :(得分:3)

$('a:has(input)')

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

答案 3 :(得分:3)

有(至少)两种方法可以实现这一目标:

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