简体   繁体   English

jQuery删除 <p> 从成功fn标记

[英]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:- 这种方法的优点:

  • Its more simple than using regular expressions. 它比使用正则表达式更简单。
  • If your response comes as <p id='paraId'> data </p> . 如果您的响应来自<p id='paraId'> data </p> It will still run. 它将仍然运行。
  • Take this case <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的清洁程度。
  • If your response changes tomorrow and you expect any other HTML tag too then you need to update your regex which can be quite complex to handle. 如果明天您的响应发生变化,并且您还期望其他HTML标记,那么您需要更新正则表达式,这可能非常复杂。

Try this out:- http://jsfiddle.net/adiioo7/b5bG8/ 试试看: -http : //jsfiddle.net/adiioo7/b5bG8/

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.

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