希望能夠利用javascript解析html字串,判斷所有的<img>元素,如果結尾有加上/>,就像是<img />的話,就刪除。假設現在有一組字串:<h1>h1</h1><img id="a" src="https://placehold.it/200x200" alt="" /><h2>h2</h2><img id="b" src="https://placehold.it/200x200" alt="">希望能夠改為<h1>h1</h1><img id="a" src="https://placehold.it/200x200" alt=""><h2>h2</h2><img id="b" src="https://placehold.it/200x200" alt="">我嘗試使用jquery做判斷,發現讀入jquery時,似乎好像已經被標準化了,無法從jquery對象中判斷原本的img tag 的html。html<img id="a" src="https://placehold.it/200x200" alt="" /> <img id="b" src="https://placehold.it/200x200" alt="">javascriptconsole.log($("#a"));console.log($("#b")); 兩個打印出來的元素是一模一樣的。在python中的beautifulSoup和tag中,有個接口叫做isSelfClosing正是我需要的,請問這個在javascript有類似的庫和接口嗎?我找了jquery和cheerio的文檔都沒找到
如何用javascript解析html判斷該元素最后有沒有加上 / >
拉莫斯之舞
2018-11-29 22:15:13