第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將 HTML <div..../div> 轉(zhuǎn)換為具有 name:value 屬性的

如何將 HTML <div..../div> 轉(zhuǎn)換為具有 name:value 屬性的

我有一個(gè)包含多個(gè) HTML div 的文本文件,例如<div id="ID_a" name="VEC" class="cat" title ="scene,-12, 0.5, 0, 1" text = "$\omega =9$">   </div>  <div id="ID_b" name="VEC" class="cat" title ="scene,-12, 0.5, 0, 3" text = "$\alfa = 2$">   </div>  <div id="ID_c" name="VEC" class="cat" title ="scene,-12, 0.5, 0, 4" text = "$\beta = 30.5$"></div>  <div id="ID_d" name="VEC" class="cat" title ="scene,-12, 0.5, 0, 2" text = "$\gamma = 90$"> </div>  我希望將<div ...> </div>文本中的每個(gè)語(yǔ)句(使用 javascript)轉(zhuǎn)換為 javascript 對(duì)象規(guī)范語(yǔ)句,例如Coll[1] = {id:"ID_a", name: "VEC", class: "cat", title:"scene,-12, 0.5, 0, 1", text = "$\omega =9$" }; Coll[2] = {id:"ID_a", name: "VEC", class: "cat", title:"scene,-12, 0.5, 0, 3", text = "$\alfa = 2$" }; Coll[3] = {id:"ID_a", name: "VEC", class: "cat", title:"scene,-12, 0.5, 0, 4", text = "$\beta = 30.5$" }; Coll[4] = {id:"ID_a", name: "VEC", class: "cat", title:"scene,-12, 0.5, 0, 2", text = "$\gamma = 90$" }; 我想知道是否有一種簡(jiǎn)單的方法可以做到這一點(diǎn)?編輯:我想在不使用 DOM 模型或?qū)?div 作為 html 文件的一部分加載的情況下使用純 javascript 來(lái)完成。抱歉之前沒有說(shuō)清楚。
查看完整描述

2 回答

?
波斯汪

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是你想要的?


let divs = document.querySelectorAll('div');


let obj = [];


divs.forEach(element => {

  let e = {

    id: element.id,

    name: element.getAttribute("name"),

    class: element.className,

    title: element.title,

    text: element.getAttribute("text")

  };

  obj.push(e);

})


console.log(obj)

<div id="ID_a" name="VEC" class="cat" title="scene,-12, 0.5, 0, 1" text="$\omega =9$"> </div>

<div id="ID_b" name="VEC" class="cat" title="scene,-12, 0.5, 0, 3" text="$\alfa = 2$"> </div>

<div id="ID_c" name="VEC" class="cat" title="scene,-12, 0.5, 0, 4" text="$\beta = 30.5$"></div>

<div id="ID_d" name="VEC" class="cat" title="scene,-12, 0.5, 0, 2" text="$\gamma = 90$"> </div>


查看完整回答
反對(duì) 回復(fù) 2023-06-15
?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊

假設(shè)您將所有 div 作為字符串加載,您可以將每個(gè)字符串映射到一個(gè)對(duì)象。這可以通過(guò)按每個(gè)屬性拆分字符串(通過(guò)regex)然后通過(guò)reduce將這些字符串轉(zhuǎn)換為對(duì)象來(lái)完成。

因?yàn)槟承┲蛋忍?hào),所以我搜索第一次出現(xiàn)的索引,并在將其作為對(duì)象返回之前將字符串拆分為帶有切片的鍵和值。

const divs = [

? '<div id="ID_a" name="VEC" class="cat" title ="scene,-12, 0.5, 0, 1" text = "$\omega =9$">? ?</div>',

? '<div id="ID_b" name="VEC" class="cat" title ="scene,-12, 0.5, 0, 3" text = "$\alfa = 2$">? ?</div>',

? '<div id="ID_c" name="VEC" class="cat" title ="scene,-12, 0.5, 0, 4" text = "$\beta = 30.5$"></div>',

? '<div id="ID_d" name="VEC" class="cat" title ="scene,-12, 0.5, 0, 2" text = "$\gamma = 90$"> </div>',

];


const Coll = divs.map((div) => {

? const parts = div.match(/[\w-]+\s?=\s?"[^"]*"/g);


? const objectified = parts.reduce((obj, str) => {

? ? const index = str.indexOf("=");

? ? const key = str.slice(0, index);

? ? const value = str.slice(index + 1, str.length);


? ? obj[key] = value;


? ? return obj;

? }, {});

??

? return objectified;

});


console.log("Single value:", Coll[0].id);

console.log(Coll);

.as-console-wrapper { max-height: 100% !important; }


查看完整回答
反對(duì) 回復(fù) 2023-06-15
  • 2 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)