简体   繁体   中英

How to get prefix name from html tag

I have some input html tag like this
Input:

<div fx-prop sc-alias="" fx-type="Organization"><div fx-name="Kirito">SAO <i fx-name="Asuna">World</i></div></div>

Is there any ways to get attribute that contains scheme prefix ​ fx-​​ is tag with scheme from that input

What I want to do is get output like this
Output:

[
  {"prop":"", alias:"", "type":"Organization"},
  [
    {"name":"Kirito"},
    [
      {"name":"Asuna"}
    ]
  ]
]

The solution maybe using php or javascript, but I still don't get the answer

If you know the attribute names, you can use the Element.getAttribute() method in conjunction with Element.attributes :

 const elt = document.getElementsByTagName('div')[0]; const func = elt => { let res=[{}]; for (let i=0; i<elt.attributes.length; i++) { let attrName = elt.attributes[i].name; if (attrName.substring(0,2)=='fx') { res[0][attrName.substring(3)] = elt.getAttribute(attrName); } } for (let i=0; i<elt.children.length; i++) { res.push(func(elt.children[i])); } return res; } console.log(func(elt)); 
 <div fx-prop sc-alias="" fx-type="Organization"> <div fx-name="Kirito"> SAO <i fx-name="Asuna"> World </i> </div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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