使用Javascript选择div导致页面未找到错误 [英] Using Javascript to select div leads to page not found error

查看:97
本文介绍了使用Javascript选择div导致页面未找到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法在另一个线程上获得了帮助,以使用JavaScript更改元素的标题和链接(

I managed to get help on another thread for changing the title and link of an element using JavaScript (Issue with changing the title of a link using jQuery).

我曾尝试在网站上的另一元素上实现该技术,但遇到错误Error 404: The page your are looking for cannot be found.

I have tried implementing the technique there to another element on my website but am instead met with an error Error 404: The page your are looking for cannot be found.

因此,尽管此代码是独立工作的:

Thus, while this code works in isolation:

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);

以下代码导致上述错误.

The following code leads to the error mentioned above.

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代码的链接(链接).请注意,该链接仅反映与无法正常运行的javascript代码相关的html. href2.我尚未发布href定位的细分受众群,因为它可能太混乱了.我的目的是用词Click Here替换www.hotmail.com,但是用户应该能够选择Click Here,它将直接将它们定向到www.hotmail.com.另外,由于我只需要选择html的相关部分,因此您在链接中查看的选择器将与上面我的代码中所述的选择器不同.

Attached is the link to the html code I am targeting (Link). Note that the link only reflects the html related to the javascript code that I can't get working ie. href2. I had not posted the segment that href was targeting as it might get too messy. My purpose is to replace www.hotmail.com with the words Click Here, but the user should be able to select Click Here and it would direct them to www.hotmail.com. Also, because I had to select only the relevant portions of the html, the selector you view in the link would be different from what is stated in my code above.

谢谢您的帮助.

推荐答案

使用:eq()选择器选择第四个li,然后替换其内容:

Use :eq() Selector to select the 4th li and then replace its content:

HTML:

 <div id="buttons medium button-outlined">https://www.hotmail.com</div>

jQuery:

var href2 = $('.container li:eq(3) > .buttons').html();
var link2 = "<a href='"+href2+"' target='_blank'>Click Here</a>";
$('.container li:eq(3) > .buttons').replaceWith(link2);

工作演示此处

这篇关于使用Javascript选择div导致页面未找到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆