【发布时间】:2018-06-07 06:42:38
【问题描述】:
我设法在另一个线程上获得帮助,以使用 JavaScript (Issue with changing the title of a link using jQuery) 更改元素的标题和链接。
我曾尝试在我网站上的另一个元素上实施该技术,但遇到了错误Error 404: The page your are looking for cannot be found.
因此,虽然这段代码是独立工作的:
var href = jQuery('#_tab_1 > div > div > div:nth-child(3) > div > div.pf-body > ul > li:nth-last-child(1) > p.item-property').html();
var link = "<a href='"+href+"' target='_blank'>Click Here</a>";
jQuery('#_tab_1 > div > div > div:nth-child(3) > div > div.pf-body > ul > li:nth-last-child(1) > p.item-property').replaceWith(link);
以下代码导致上述错误。
var href = jQuery('#_tab_1 > div > div > div:nth-child(3) > div > div.pf-body > ul > li:nth-last-child(1) > p.item-property').html();
var link = "<a href='"+href+"' target='_blank'>Click Here</a>";
jQuery('#_tab_1 > div > div > div:nth-child(3) > div > div.pf-body > ul > li:nth-last-child(1) > p.item-property').replaceWith(link);
var href2 = jQuery('#c27-single-listing > section > div.profile-cover-content.reveal.reveal_visible > div > div > ul > li:nth-child(3) > div').html();
var link2 = "<a href='"+href2+"' target='_blank'>Click Here</a>";
jQuery('#c27-single-listing > section > div.profile-cover-content.reveal.reveal_visible > div > div > ul > li:nth-child(3) > div').replaceWith(link2);
附件是指向我要定位的 html 代码的链接 (Link)。请注意,该链接仅反映与我无法正常工作的 javascript 代码相关的 html。 href2。我没有发布 href 所针对的部分,因为它可能会变得太乱。我的目的是将www.hotmail.com 替换为Click Here,但用户应该能够选择Click Here,它会将他们定向到www.hotmail.com。另外,因为我必须只选择 html 的相关部分,所以您在链接中查看的选择器将与我上面代码中所述的不同。
感谢您的帮助。
【问题讨论】:
-
查看
href2的内容。它是有效的网址吗?href2定义的资源是否存在? -
错误提示您请求的页面未找到。你如何重现错误?您是否以某种方式点击了链接 2?
-
请出示您的html代码
-
好像你的选择器太复杂了。尝试给你想要选择的项目替换一个特定的
class或特定的id并且只使用`jQuery('#replace-later').replaceWith(link) 相同的第二个选择器。我认为您的大多数问题可能与未找到实际页面元素的选择器有关 -
请添加minimal, complete, and verifiable example 以显示实际问题。链接的小提琴没有多大意义......
标签: javascript jquery http-status-code-404