Pau*_*uck 17 html javascript php jquery
可能重复:
查找元素是否存在于整个html页面中
我想做点事情:
HTML:
<span id="one">one</span>
<span id="two">two</span>
<span id="three">three</span>
Run Code Online (Sandbox Code Playgroud)
JavaScript:
if (isset($("#one"))){
alert('yes');
}
if (isset($("#two"))){
alert('yes');
}
if (isset($("#three"))){
alert('yes');
}
if (!isset($("#four"))){
alert('no');
}
Run Code Online (Sandbox Code Playgroud)
生活:
我该怎么做?
Sni*_*sie 28
if (($("#one").length > 0)){
alert('yes');
}
if (($("#two").length > 0)){
alert('yes');
}
if (($("#three").length > 0)){
alert('yes');
}
if (($("#four")).length == 0){
alert('no');
}
Run Code Online (Sandbox Code Playgroud)
这就是你需要的:)
rea*_*dow 18
你可以使用length:
if($("#one").length) { // 0 == false; >0 == true
alert('yes');
}
Run Code Online (Sandbox Code Playgroud)
function isset(element) {
return element.length > 0;
}
Run Code Online (Sandbox Code Playgroud)
或者,作为jQuery扩展:
$.fn.exists = function() { return this.length > 0; };
// later ...
if ( $("#id").exists() ) {
// do something
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
100454 次 |
| 最近记录: |