【问题标题】:Select children link with jquery [closed]选择带有jquery的子链接[关闭]
【发布时间】:2017-01-27 08:55:32
【问题描述】:

应该如何用jquery在这个结构中选择链接<a>

<span id="guiaAtencion:ot4">
  <p>
    <a href="http://www.apps.com">link</a>
  </p>
</span>

【问题讨论】:

  • 这完全取决于您尝试从哪个元素和事件中选择它。我们需要更多信息
  • 除非您提供更多信息,否则这是您的答案 $("a")
  • 您的时间值得从头到尾阅读the jQuery API。只需要一个小时,也许两个小时,它立即回报你。

标签: jquery html hyperlink jquery-selectors


【解决方案1】:

您可以使用 span 的 id 一个锚点,并使用 descendant combinator 在其中找到 a

var theLink = $("#guiaAtencion\\:ot4 a");

示例

$("#guiaAtencion\\:ot4 a").css("color", "green");
<span id="guiaAtencion:ot4">
  <p>
    <a href="http://www.apps.com">link</a>
  </p>
</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

或者

var theLink = $("[id='guiaAtencion:ot4'] a");

示例

$("[id='guiaAtencion:ot4'] a").css("color", "green");
<span id="guiaAtencion:ot4">
  <p>
    <a href="http://www.apps.com">link</a>
  </p>
</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

旁注:将p 元素放在span 元素中是无效的。 span 的内容模型(例如,允许在其中包含的内容)是 phrasing content,但 p 元素只能在需要 flow content 的地方使用。

【讨论】:

    猜你喜欢
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多