简体   繁体   中英

Display Based on innerHtml number

I want to create a system that displays certain content based on the number for this innerhtml content... Here's the actual element itself, 17 is just the number for mine it is different for each user:

<span id="your_div_id_diamonds"><dd><div class="field_uneditable">17</div></dd></span>

I want it to display if their number is say between 10 and 20... Here's a code I've been trying to work with, but it only does one number at a time and currently isn't working...

$(function() {


if(document.getElementById('your_div_id_diamonds').innerHTML = "17") {
     document.getElementById('elitecontent').className="gotelite";
}
 else {
        document.getElementById('elitecontent').style.display="none";
    }
}



});

Here's a version that works, but again only works for one number at a time... It'd be a huge pain if I had it go up to say 150 or 200, I'd have to make like 200 else if statements.

        $( "#lev1" ).load('/u' + _userdata.user_id + ' #field_id-14 dd', function() {

var divs= document.getElementsByClassName('field_uneditable');

for (var i = 0, len = divs.length; i < len; ++i) {

if(divs[i].innerHTML.indexOf("7") != 1) {
      document.getElementById('elitecontent').innerHTML="Elite";
}

else if(divs[i].innerHTML.indexOf("16") != -1) {
      document.getElementById('elitecontent').innerHTML="Elite";
}


else if(divs[i].innerHTML.indexOf("17") != -1) {
      document.getElementById('elitecontent').innerHTML="Elite";
}
 else {
      document.getElementById('elitecontent').innerHTML="Starter";
    }
}



});

I basically want a code that works similar to with values, where I can just put something like >=10 and =<20

The problem you are facing with your current code is that you aren't using the correct comparison statements = is declarative, not used for comparison. In its place you should be using ==(matches regardless of data type) or === (must match data type as well) for instance

$(function() {

if(document.getElementById('your_div_id_diamonds').innerHTML = "17") {
     document.getElementById('elitecontent').className="gotelite";
}else {
     document.getElementById('elitecontent').style.display="none";
}
}

});

should be

$(function() {

if(document.getElementById('your_div_id_diamonds').innerHTML == "17") {
     document.getElementById('elitecontent').className="gotelite";
}else {
     document.getElementById('elitecontent').style.display="none";
}
}

});

However, for your needs something along the lines of:

$(function() {

if(document.getElementById('your_div_id_diamonds').innerHTML <=20 && document.getElementById('your_div_id_diamonds').innerHTML >=10) {
     document.getElementById('elitecontent').className="gotelite";
}else {
     document.getElementById('elitecontent').style.display="none";
}
}

});

should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM