How do I do this simple task in JQuery...show an image
I have many of these loading signs.
<span id="loading" rel="1n" style="display:none;">Loading...</span>
<span id="loading" rel="2a" style="display:none;">Loading...</span>
<span id="l开发者_如何学Pythonoading" rel="3w" style="display:none;">Loading...</span>
Notice the only difference is rel.
How do I write a JQuery script so that I choose which one to show?:
$("#loading").show() where rel = "2a"?
tries in this way:
$("#loading[rel='2a']").show();
Edit
Quite simply, no matter the language, is a simple concatenation of strings
var yourVariable = "2a";
$("#loading[rel='"+yourVariable+"']").show();
if you want to show all spans with a rel attribute that is "2a"
$('span[rel=2a]').show();
to show all spans that have a rel attribute that begins with "2a"
$('span[rel^=2a]').show();
More info on selectors
also, id's should be unique per element so I would recommend using classes to mark loading images instead of id.
so you would have
<span rel="1n" style="display:none;" class="loading-message">Loading...</span>
<span rel="2a" style="display:none;" class="loading-message">Loading...</span>
<span rel="3w" style="display:none;" class="loading-message">Loading...</span>
and your jquery would be:
$('.loading-message[rel^=2a]').show();
Update: to do a variable just do:
var x = "2a";
$('span[rel^='+x+']').show();
$('#loading[rel=1n]').show();
But, you really shouldn't have repeated ID's in your dom.
精彩评论