[英]Jquery removing <p> tag from success fn
I get response in my ajax success fn. 我的ajax成功获得了回应。 My data variable gives me
<p>hello</p>
. 我的数据变量给我
<p>hello</p>
。
How do I remove <p>
and </p>
from my data variable ? 如何从数据变量中删除
<p>
和</p>
?
I used .remove()
but it is not working. 我使用了
.remove()
但是它不起作用。
Try this: 尝试这个:
var data = '<p>hello</p>'
var text = $(data).text();
console.log(text);
.remove()
removes elements from the DOM, not from strings. .remove()
从DOM中删除元素,而不是从字符串中删除元素。
If you want to delete the <p>
you can add the results to the DOM as hidden then remove the <p>
and only then show it. 如果要删除
<p>
,则可以将结果以隐藏的形式添加到DOM中,然后删除<p>
并仅显示它。
You can also try this example . 您也可以尝试以下示例 。 There will be no need to remove P tags.
无需删除P标签。
var content = $(ajaxResponseString); //content = $("<p>Data</p>");
var html = content.html();
Advantages of this method:- 这种方法的优点:
<p id='paraId'> data </p>
. <p id='paraId'> data </p>
。 It will still run. <p > data </p>
, this approach will run fine while regex may/may not depending upon how clean your regex is. <p > data </p>
,这种方法可以很好地运行,而regex可以/可以不依赖于regex的清洁程度。 JS:- JS:-
var html = "<p>Some HTML</p>";
var div = document.createElement("div");
div.innerHTML = html;
var text = div.textContent || div.innerText || "";
alert(text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.