Javascript 试图通过html查找文本并更改锚定标记失败
我在一个div中有下面的javascript,并试图找到它来用新类型的标记替换一个完整的a标记 这是我的htmlJavascript 试图通过html查找文本并更改锚定标记失败,javascript,jquery,Javascript,Jquery,我在一个div中有下面的javascript,并试图找到它来用新类型的标记替换一个完整的a标记 这是我的html <div class="info-more"><h3>We're here to help!</h3><p>Sales: 1-800-555-5555</p><p>Support: 1-888-555-6655</p><a href="https://website.com"
<div class="info-more"><h3>We're here to help!</h3><p>Sales: 1-800-555-5555</p><p>Support: 1-888-555-6655</p><a href="https://website.com" target="_blank">Click here to chat with us</a></div>
$(document).ready(function() {
console.log($(document).find(".info-more").text());
$(".info-more").closest("a").attr("href","javascript:;");
});
您可以使用.find而不是.closest。将查看selector元素的子代,而在selector元素向上遍历其祖先以查找选择器时,将只查看selector元素的父代
请参见下面的工作示例:
$document.readyfunction{
console.log$.info-more.finda.text;
$.info-more.finda.attrref,javascript:;;
};
我们是来帮忙的!
销售:1-800-555-5555
支持:1-888-555-6655
使用.find代替.closest。“查找”将查找子元素。。。而.closest则查找父元素;刚刚尝试了您的代码,控制台显示为空。我尝试了文本或html两种$document.readyfunction{console.log$document.find.info-more.finda.text;$.info-more.finda.attrref,javascript:;;};当我运行您给我的代码时,它会单击此处在控制台中与我们聊天。这是你想要的吗?它还更改了a href链接。运行上面的代码片段。它应该产生相同的输出。