简体   繁体   English

如何检查HTML中是否存在这样的元素(锚元素是否存在)?

[英]How can I check whether such a element is exists in the HTML which is exists it the anchor?

I have a URL plus a anchor in the end of that, something like this: 我在末尾有一个URL和一个锚,如下所示:

www.example.com/classname/methodname/arg#idname

Now I want to know, is there any element in the HTML which has id="idname" ? 现在我想知道,HTML中是否有任何具有id="idname"元素? How can I do that? 我怎样才能做到这一点?

You can also parse with vanilla JS 您也可以使用Vanilla JS解析

var raw = www.example.com/classname/methodname/arg#idname;
var exists = false;
try {
    var arrRaw = raw.split('#');
    if (arrRaw[1]) {
       if ( arrRaw[1] === 'idname') { exists = true}
    } else {
      exists = false;
    }       
} catch (error) {
    exists = false;        
}
if ($('#idname').length){
   //it exists
} else
{
   //it doesn't
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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