Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何选择几个CSS类并应用Javascript逻辑?_Javascript_Html_Css_Button_Anchor - Fatal编程技术网

如何选择几个CSS类并应用Javascript逻辑?

如何选择几个CSS类并应用Javascript逻辑?,javascript,html,css,button,anchor,Javascript,Html,Css,Button,Anchor,在一个网站上,我有几个按钮要求演示。 每个按钮都是指向同一链接的锚定标记,即mailto方向。电子邮件方向的更改取决于您选择的国家/地区。 所以我提出了这个逻辑: function changeEmailAdress(){ if(window.location.href.indexOf("#ar") != -1){ document.querySelector(".mailToRef").href="m

在一个网站上,我有几个按钮要求演示。 每个按钮都是指向同一链接的锚定标记,即mailto方向。电子邮件方向的更改取决于您选择的国家/地区。 所以我提出了这个逻辑:

    function changeEmailAdress(){
        if(window.location.href.indexOf("#ar") != -1){
          document.querySelector(".mailToRef").href="mailto:argentina@globalnewsgroup.com";
        }else if(window.location.href.indexOf("#br") != -1){
          document.querySelector(".mailToRef").href="mailto:brasil@globalnewsgroup.com";
        }else if(window.location.href.indexOf("#us") != -1){
          document.querySelector(".mailToRef").href="mailto:usa@globalnewsgroup.com";
        }else{
          document.querySelector(".mailToRef").href="mailto:demo@globalnewsgroup.com";
        }
    }
这是html:

<a id="mailto" href="" target="_blank" class="cta cta--accent mailToRef">Solicitar Demo</a>

<a href="" class="c-navigation__item mailToRef">Contacto</a>

<a href="" target="_blank" class="cta cta--lg mailToRef" data-solicitude="demo" ">Accedé a una Demo</a>

只有当我有一个带有“mailto”类的锚标记时,Javascript代码才能完美工作。然后,当我将该类添加到其他链接时,它就不起作用了

我尝试使用“querySelectorAll”,但它也不起作用。 我怎样才能让它工作?我想我遗漏了什么,但我不知道是什么

提前谢谢。

您应该使用


它返回一个数组,因此您应该使用
forEach
在其中循环。

您需要使用
mailToRef
类选择所有元素,并根据它们的索引分配各个锚定

let anchors = document.querySelectorAll(".mailToRef");
function changeEmailAdress(){
    if(window.location.href.indexOf("#ar") != -1){
      anchors[0].href="mailto:argentina@globalnewsgroup.com";
    }else if(window.location.href.indexOf("#br") != -1){
      anchors[1].href="mailto:brasil@globalnewsgroup.com";
    }else if(window.location.href.indexOf("#us") != -1){
      anchors[2].href="mailto:usa@globalnewsgroup.com";
    }else{
      anchors[3].href="mailto:demo@globalnewsgroup.com";
    }
}

OP不希望所有锚定标记的href都相同。“你的答案是这样的。”事实上,我是这样解释的。OP希望将您发送到
巴西
演示,如果您单击巴西,请联系。为什么向下滚动到巴西部分(在OPs代码中用#ar/br等表示)会给您发送非巴西的电子邮件?
let anchors = document.querySelectorAll(".mailToRef");
function changeEmailAdress(){
    if(window.location.href.indexOf("#ar") != -1){
      anchors[0].href="mailto:argentina@globalnewsgroup.com";
    }else if(window.location.href.indexOf("#br") != -1){
      anchors[1].href="mailto:brasil@globalnewsgroup.com";
    }else if(window.location.href.indexOf("#us") != -1){
      anchors[2].href="mailto:usa@globalnewsgroup.com";
    }else{
      anchors[3].href="mailto:demo@globalnewsgroup.com";
    }
}