开发者

How to detect the class of a link in my javascript?

开发者 https://www.devze.com 2023-03-13 12:19 出处:网络
Hi I have this script: function ajax(){ if (navigator.standalone) return; for (var i= document.links.length; i-->0;) {

Hi I have this script:

function ajax(){
    if (navigator.standalone) return;
    for (var i= document.links.length; i-->0;) {
        document.links[i].onclick= function() {
            var req= new XMLHttpRequest();
            req.onreadystatechange= function() {
                if (this.readyState!==4) return;
                document.body.innerHTML= this.responseText;
                ajax();
            };
            req.open('GET', this.href, true);
            req.send();
            return false;
        };}
    }



window.onload= function() {
    window.scrollTo(0, 0.9);
    ajax();

};

Now I just want to add 1 little thing. If a link has a class called "noeffect" the ajax should not execute (annother page should load).I've tried something but i Am just not good enough with javascript to get this right:

function ajax(){
    if (navigator.standalone) return;
    for (var i= document.links.length; i-->0;) {
        if (document.links[i].getAttribute("class") == "noeffect") return;
        document.links[i].onclick= function() {
            var req= new XMLHttpRequest();
            req.onreadystatechange= function() {
                if (this.readyState!==4) return;
                document.body.innerHTML= this.responseText;
                ajax();
            };
            req.open('开发者_如何转开发GET', this.href, true);
            req.send();
            return false;
        };}
    }



window.onload= function() {
    window.scrollTo(0, 0.9);
    ajax();

};

I guess it checkes all links instead of each one but I don't know how to code this the right way. :s


Figured it out!

function ajax(){
    if (navigator.standalone) return;
    for (var i= document.links.length; i-->0;) {
        document.links[i].onclick= function() {
            if(this.getAttribute("class") == "noeffect") return;
            var req= new XMLHttpRequest();
            req.onreadystatechange= function() {
                if (this.readyState!==4) return;
                document.body.innerHTML= this.responseText;
                ajax();
            };
            req.open('GET', this.href, true);
            req.send();
            return false;
        };}
    }



window.onload= function() {
    window.scrollTo(0, 0.9);
    ajax();

};
0

精彩评论

暂无评论...
验证码 换一张
取 消