<my-tag data-custom-attribute="value"></my-tag>
<script>
var myTag = document.querySelector("my-tag")
var customAttributeValue = myTag.dataset.customAttribute
console.log(customAttributeValue)// Output: "value"
</script>
一、获取img标签的src属性值
var imgStr = "<p><img src='http://www.baidu.com/FpmF-JifNksQTHo7InP_LMukbtWc'/></p>"
//解析富文本获取img里面的src属性值
var patt = /<img[^>]+src=['"]([^'"]+)['"]+/g
var result = [],
temp
while ((temp = patt.exec(imgStr)) != null) {
result.push({'url':temp[1]})
}
console.log(result) //http://www.baidu.com/FpmF-JifNksQTHo7InP_LMukbtWc
二、获取指定元素属性值
function getDomAttr(str,tag,attr) {
// const val = /(?<=data-value=").*?(?=")/
var patt = `/<${tag}[^>]+${attr}=['"]([^'"]+)['"]+/g`
let temp = patt.exec(str)
// console.log('str.match(val)', temp)
return temp[1]
}