gpt4 book ai didi

javascript:从html中提取文本

转载 作者:行者123 更新时间:2023-11-30 10:50:52 24 4
gpt4 key购买 nike

我得到的 html 内容如下:

var test='<div id="test">Raj</div>';

我如何使用 javascript 从上面的 html 内容中检索值 Raj。

最佳答案

听起来您正试图从该 HTML 片段中提取文本 "Raj"

要让浏览器的 HTML 解析器为您完成肮脏的工作:

// create an empty div
var div = document.createElement("div");

// fill it with your HTML
div.innerHTML = test;

// find the element whose text you want
test = div.getElementById("test");

// extract the text (innerText for IE, textContent for everyone else)
test = test.innerText || test.textContent;

或者在 jQuery 中:

test = $(test).text();

关于javascript:从html中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5398201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com