我如何测试对象(例如 Image 、 Div 或 Table ...)的 Id 是否在 html 代码中。 ID可以是例如如下:id=My_Img_Id or id="My_Img_Id"or id="My_Img_Id"or id=My_Img_Id
var sHTML = '... any Text abc < IMG src="path/myfile.jpg" id="My_Img_Id" style="..." > ... any Text';
var sId = 'My_Img_Id';
var sReg = '<.*? *id*\= *\"*['+sId+' *"*]$.*';
var sRegExp = new RegExp(sReg, "g");
var Result = sRegExp.test( sHTML );
alert( Result );
非常感谢。
最佳答案
首先将 html 字符串转换为 jQuery 对象,然后您可以使用 find 从 jQuery 对象中获取元素。如果 length 属性大于零则使用 id 元素存在于 html 字符串中。
$(sHTML).find('#' +sId ).length
关于javascript - 通过正则表达式从 HTML 获取 Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24449291/