【问题标题】:Using Javascript to select div leads to page not found error使用 Javascript 选择 div 导致页面未找到错误
【发布时间】: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


【解决方案1】:

这是一个可以满足您需求的 sn-p。 请注意,您可以使用类(用于多个元素)或 id(用于单个元素)。

基本上,我使用了几个命令来做你想做的事:

  • each() - 应该为选择器找到的每个元素执行以下功能。 this 将引用 DOM 元素
  • text() - 返回该项目的文本(不含 html)
  • replaceWith() - 用你想要的替换元素

在这个例子中,每个类 should-become-link 的项目都会在 2 秒后变成一个链接(因为 setTimeout)。 元素内的原始文本将用作新的href 值。

$(document).ready(function() {
  setTimeout(function() {
    var element = $('.should-become-link');
    element.each(function(index) {
      var $this = $(this);
      var currentText = $this.text();

      $this.replaceWith(
        `<a href="${currentText}" target="_blank">Click Here</a>`
      );
    });
  }, 2000);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="buttons medium button-outlined should-become-link">
  https://www.hotmail.com
</div>

<div><a href="http://www.google.com" target="_blank">shouldn't change!</a></div>
<div><a href="//www.google.com" target="_blank">shouldn't change!</a></div>
<div><a href="//www.google.com" target="_blank">shouldn't change!</a></div>
<div><a href="//www.google.com" target="_blank">shouldn't change!</a></div>
<div><a href="//www.google.com" target="_blank">shouldn't change!</a></div>

<div class="buttons medium button-outlined should-become-link">
  https://www.google.com
</div>

这是codepen

【讨论】:

  • 如果这不能完全回答您的问题,请指出缺少的内容,我将编辑我的答案
  • 您好 Thatkookooguy,我尝试实施您的解决方案,但无法使其正常工作。你能帮我看看下面的链接有什么问题吗? link。谢谢
  • @firefirehelphelp 您只是忘记将 jQuery 作为依赖项包含在内。这是一个固定的jsfiddle。如果您只想更改 Hotmail 按钮,请给它一个唯一的类或 ID 并将其添加到选择器
  • 您好 Thatkookooguy,我已尝试修改代码,请参阅 link。您能帮我解决以下问题吗(1)当我将上面的代码粘贴到我的 Wordpress 主题的自定义代码部分并选择更新时,它仍然不会将 www.hotmail.com 更改为 Click Here。 (2) 另外,我不相信我可以给 www.hotmail.com 一个唯一的类或 ID,因为这是由我使用的 wordpress 主题生成的? (3) 另外,当我使用 Internet Explorer 访问我提供的链接时,它不会将文本更改为单击此处。是否有一个原因?谢谢。
【解决方案2】:

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

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

工作演示here

【讨论】:

  • 感谢您的回复。好像您将 div 的名称更改为 link2。我试图让它使用 div 的原始名称来工作,但没有成功。你能帮帮我吗?附上我的作品 --> link 谢谢。
  • 您好 YouneL,感谢您的回复。我已将您的代码输入到我的 wordpress 主题的 javascript 部分 -->link。但是,它仍然没有将www.hotmail.com 更改为Click Here --> link。这与将 Jquery 作为依赖项有什么关系吗?如果是这样,您能否让我知道如何修改我的代码以包含此内容?谢谢。
  • 您的html代码中有多少个容器类,请显示javascript错误可能有助于解决您的prb
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多