任何JavaScript GURU在这里做这个脚本?如果更改锚点重定向页面 [英] Any Javascript GURU here to make this Script ? If Anchor is Changed Redirect the Page

查看:95
本文介绍了任何JavaScript GURU在这里做这个脚本?如果更改锚点重定向页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript专家,

我在自己创建的模板中使用了一个链接......正如你看到的那样:

 < a href ='http://www.allbloggertricks.com'>博客提示< / a> 

不需要,如果要将锚文本博客



示例:如果链接变成这样:

 < a href ='http://www.allbloggertricks.com'> Awesome Tips< / a> 

现在页面应该重定向到example.com,因为它的锚文本从更改了博客提示改为

至$ / $>

精彩提示

解决方案

不确定您是希望页面自动重定向还是仅动态更改URL。当你使用'重定向'这个词时,我会选择第一个。页面加载后运行类似的内容。

  var anchorTags = document.getElementsByTagName(a); 
var len = anchorTags.length;
while(len--){
var anchor = anchorTags [len];
if(anchor.textContent ===Awesome Tips){
anchor.href =http://example.com/;
anchor.click();
}
}

希望有所帮助。 :)






编辑:进一步注释如下。

如果我理解正确,您需要确定具体的锚标记,然后检查它的文本是否为博客提示。第一件要做的就是给你的目标定位标签一个唯一的标识(ID)... ...

pre $ lt; code>< a href = http://..etc ..id =the_link>博客提示< / a>

现在您可以轻松找到它...

  var theLink = document.getElementById('the_link'); 

然后您可以进行类似于上述建议的检查,但是这次要查看文本是否不是'博客技巧'...

  if(theLink.textContent!==Blogging Tips){
// ...做点什么...
}

如果你想重定向页面最简单的方法是使用 window.location.assign 有关MDN的更多信息)。



您只能在页面加载后才进行此检查,因此您需要将其封装在 window.onload 函数或将其放在一个可以在页面加载后调用的函数中(有关MDN的更多信息)。假设第一个选项,JavaScript现在看起来像这样...

  window.onload = function(){
var theLink = document.getElementById(the_link);
if(theLink.textContent!==Blogging Tips){
window.location.assign(http://example.com/);
}
};但是,如果您的模板用户删除或更改了锚点标记的ID,或者更改了JavaScript,则几乎不存在你可以这样做。



希望有更多的帮助。 :)

Javascript expert,

i used a link in my own created templates...as you see the below ones:

<a href='http://www.allbloggertricks.com'>Blogging Tips</a>

No, i want if change the anchor text from Blogging Tips to any other then the page should be redirected to example.com .

Example: if the link become like this:

<a href='http://www.allbloggertricks.com'>Awesome Tips</a>

Now the page should be redirect to example.com because its anchor text changed from "Blogging Tips" to

"Awesome Tips"

解决方案

Not sure if you want the page to automatically redirect or to just have the URL changed dynamically. As you used the term 'redirect' I'll go with the first one. Run something like this once your page has loaded.

var anchorTags = document.getElementsByTagName("a");
var len = anchorTags.length;
while (len--) {
    var anchor = anchorTags[len];
    if (anchor.textContent === "Awesome Tips") {
        anchor.href = "http://example.com/";
        anchor.click();
    }
}

Hope that helped. :)


EDIT: Further to the comment below.

If I understand you correctly, you'll need to identify the specific anchor tag and then check to see if it's text is "Blogging Tips", or not. The first thing to do is give your target anchor tag a unique identification (id)...

<a href="http://..etc.." id="the_link">Blogging Tips</a>

Now you can easily find it with...

var theLink = document.getElementById('the_link');

You can then do a check similar to that suggestion above, but this time to see if the text is NOT 'Blogging Tips'...

if (theLink.textContent !== "Blogging Tips") {
   // .... do something ...
}

If you want to redirect the page the easiest thing to do is use window.location.assign(More on this at MDN).

You can only make this check once the page has loaded, so you'll need to wrap this inside a window.onload function or put it in a function that can be called once the page is loaded (More on this at MDN). Assuming the first option, the JavaScript now looks like this...

window.onload = function() {
  var theLink = document.getElementById("the_link");
  if (theLink.textContent !== "Blogging Tips") {
    window.location.assign("http://example.com/");
  }
};

However, if your template user removes or changes the anchor tag's id, or changes your JavaScript there is little you can do about it.

Hope that helped a bit more. :)

这篇关于任何JavaScript GURU在这里做这个脚本?如果更改锚点重定向页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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