Javascript 如何通过分配单个变量来减少此脚本中的链接数

Javascript 如何通过分配单个变量来减少此脚本中的链接数,javascript,jquery,html,css,redirect,Javascript,Jquery,Html,Css,Redirect,有没有Javascript专家能帮我写这个脚本 实际上,我是一个模板设计师,我创建免费和高级模板,我的免费模板包括非删除页脚链接,这意味着用户将不得不保留我的模板页脚链接 说到要点 这是我的剧本: <script> window.onload = function() { var e = document.getElementById("credits"); setInterval(function() { // check every 2 seconds

有没有Javascript专家能帮我写这个脚本

实际上,我是一个模板设计师,我创建免费和高级模板,我的免费模板包括非删除页脚链接,这意味着用户将不得不保留我的模板页脚链接

说到要点

这是我的剧本:

<script>
window.onload = function() {
    var e = document.getElementById("credits");

    setInterval(function() {     // check every 2 seconds 

    if (e == null) {            // if div is removed then redirect
        window.location.href = "http://www.example.com/"
    }


                               // if div is  hidden with css visibility then redirect

    if ($(e).css('visibility') == 'hidden') {
        document.location.href = "http://www.example.com";
    }

                            // if div is display none then redirect

    if ($(e).css('display') == 'none') {
        document.location.href = "http://www.example.com";
    }


                               // if div is hidden with css collapse then redirect
    if ($(e).css('visibility') == 'collapse') {
        document.location.href = "http://www.example.com";
    }

    }, 2000) // close time function

    e.setAttribute("href", "http://www.example.com/");
    e.setAttribute("ref", "dofollow");
    e.setAttribute("title", "Blogger Templates");
    e.innerHTML = "Example"

}
</script>

<a id="credits"></a>
正如您所见,我添加了“e”变量,而不是完整链接,但它不起作用,它正在重定向到example.com/e,这是无效的。我给了你这个主意。所以让它成为可能

那么,如何做到这一点呢?我已经将“e”变量分配给我的主链接

请与我分享完整的编码,谢谢。

好的。首先,“e”不是一个变量,它是一个字符串文字。这意味着您实际上是在向document.location.href分配“e”。此外,您之前创建了一个名为“e”的变量,并将ID为“credits”的DOM元素的值赋给它

我不是100%清楚你想做什么,但以下内容可能会有所帮助:

<script>
window.onload = function() {

  var e = document.getElementById("credits");
  var redirect = 'http://www.example.com';

  // check every 2 seconds
  setInterval(function() {      

    // if div is removed then redirect
    if (e == null) {
      window.location.href = redirect
    }  

    // if div is  hidden with css visibility then redirect  
    if ($(e).css('visibility') == 'hidden') {
      document.location.href = redirect;
    }

    // if div is display none then redirect
    if ($(e).css('display') == 'none') {
      document.location.href = redirect;
    }

    // if div is hidden with css collapse then redirect
    if ($(e).css('visibility') == 'collapse') {
      document.location.href = redirect;
    }

  }, 2000) // close setInterval

  e.setAttribute("href", redirect);
  e.setAttribute("ref", "dofollow");
  e.setAttribute("title", "Blogger Templates");
  e.innerHTML = "Example"

}
</script>

window.onload=函数(){
var e=document.getElementById(“信用”);
var重定向http://www.example.com';
//每2秒检查一次
setInterval(函数(){
//如果删除了div,则重定向
如果(e==null){
window.location.href=重定向
}  
//如果使用css可见性隐藏div,则重定向
if($(e).css('visibility')=='hidden'){
document.location.href=重定向;
}
//如果div为display none,则重定向
如果($(e).css('display')=='none'){
document.location.href=重定向;
}
//如果使用css折叠隐藏div,则重定向
if($(e).css('visibility')=='collapse'){
document.location.href=重定向;
}
},2000)//关闭设置间隔
e、 setAttribute(“href”,重定向);
e、 setAttribute(“ref”、“dofollow”);
e、 setAttribute(“标题”、“博客模板”);
e、 innerHTML=“示例”
}
好的。首先,“e”不是一个变量,它是一个字符串文字。这意味着您实际上是在向document.location.href分配“e”。此外,您之前创建了一个名为“e”的变量,并将ID为“credits”的DOM元素的值赋给它

我不是100%清楚你想做什么,但以下内容可能会有所帮助:

<script>
window.onload = function() {

  var e = document.getElementById("credits");
  var redirect = 'http://www.example.com';

  // check every 2 seconds
  setInterval(function() {      

    // if div is removed then redirect
    if (e == null) {
      window.location.href = redirect
    }  

    // if div is  hidden with css visibility then redirect  
    if ($(e).css('visibility') == 'hidden') {
      document.location.href = redirect;
    }

    // if div is display none then redirect
    if ($(e).css('display') == 'none') {
      document.location.href = redirect;
    }

    // if div is hidden with css collapse then redirect
    if ($(e).css('visibility') == 'collapse') {
      document.location.href = redirect;
    }

  }, 2000) // close setInterval

  e.setAttribute("href", redirect);
  e.setAttribute("ref", "dofollow");
  e.setAttribute("title", "Blogger Templates");
  e.innerHTML = "Example"

}
</script>

window.onload=函数(){
var e=document.getElementById(“信用”);
var重定向http://www.example.com';
//每2秒检查一次
setInterval(函数(){
//如果删除了div,则重定向
如果(e==null){
window.location.href=重定向
}  
//如果使用css可见性隐藏div,则重定向
if($(e).css('visibility')=='hidden'){
document.location.href=重定向;
}
//如果div为display none,则重定向
如果($(e).css('display')=='none'){
document.location.href=重定向;
}
//如果使用css折叠隐藏div,则重定向
if($(e).css('visibility')=='collapse'){
document.location.href=重定向;
}
},2000)//关闭设置间隔
e、 setAttribute(“href”,重定向);
e、 setAttribute(“ref”、“dofollow”);
e、 setAttribute(“标题”、“博客模板”);
e、 innerHTML=“示例”
}
您添加的
“e”
不是变量,而是字符串。将其设置为
href
属性只需设置
href=“e”
,即可将您链接到当前域上的
/e

您需要使用以下任一选项:

var siteName = "http://www.example.com";

// Wherever you need it:
document.location.href = siteName;
或者只需使用
运算符,
|
,组合您的
if
语句。您可以在所有情况下重复此操作

if (e == null ||
  $(e).css('visibility') == 'hidden') {
    document.location.href = "http://www.example.com";
}
除此之外,你试图做的是一个糟糕的做法。我建议不要这样做。

您添加的
“e”
不是变量,而是字符串。将其设置为
href
属性只需设置
href=“e”
,即可将您链接到当前域上的
/e

您需要使用以下任一选项:

var siteName = "http://www.example.com";

// Wherever you need it:
document.location.href = siteName;
或者只需使用
运算符,
|
,组合您的
if
语句。您可以在所有情况下重复此操作

if (e == null ||
  $(e).css('visibility') == 'hidden') {
    document.location.href = "http://www.example.com";
}

除此之外,你试图做的是一个糟糕的做法。我建议您不要这样做。

变量
e
指向一个DOM元素,当您检查css属性等时,这是正常的,但当您试图将当前页面位置设置为同一元素(而不是某种url)时,则不正常。url是否存储在
e.href
中?我的主要问题是,要使用变量名而不是完整链接,是的,我的重定向应该只转到页脚区域的html正文中,我还将此标记添加到页脚区域。变量
e
指向一个DOM元素,当您检查css属性等时,这是正常的,但当您试图将当前页面位置设置为同一元素(而不是某种url)时,这并不正常。url是否存储在
e.href
中?我的主要问题是,要使用变量名而不是完整链接,是的,我的重定向应该只转到页脚区域的html正文中,我还将此标记添加到页脚区域。你太棒了,这正是我想要的。你干掉了这个人。啊,见鬼很高兴我能帮忙。请随意将此标记为已接受的答案,或其他什么,这样我就可以得到分数。因为我已经把我的StackOverflow档案链接到了我的投资组合网站,而且我正在使用他们的工作页面,所以这些天潜在的雇主都在看我的排名。你真是太棒了,这正是我想要的。你干掉了这个人。啊,见鬼很高兴我能帮忙。请随意将此标记为已接受的答案,或其他什么,这样我就可以得到分数。由于我已经将我的StackOverflow个人资料链接到我的投资组合网站,并且我正在使用他们的工作页面,这些天潜在的雇主都在看我的排名。谢谢你的第一个条件就是我想要的同样,谢谢你的一段代码。谢谢你的第一个条件就是我想要的