简体   繁体   English

用jQuery剥离HTML标签

[英]Stripping HTML tags with Jquery

Example: http://jsfiddle.net/axfsD/1/ 示例: http//jsfiddle.net/axfsD/1/

I'm trying to strip HTML tags in my pages. 我正在尝试剥离页面中的HTML标签。 I pasted below also two examples. 我在下面还粘贴了两个示例。 By using replace command i can't strip first button's tags, but i can't strip second one's tags. 通过使用replace命令,我不能剥离第一个按钮的标签,但是我不能剥离第二个按钮的标签。 What can be the problem? 可能是什么问题?

< script >
    $('button').replace(/<.*?>/g, "");
< /script>​

<button class="btn btn-info" rel="popover" data-content="hic   
aslinda sen de olabilir " data-original-title="Gemlik Bursa">
<small><span title="">mihan tosun</span></small></button>

<button class="btn btn-info" rel="popover" 
data-content="Benim olan benimdir Baskasinda gorduysen bil ki vazgecmisimdir   " 
data-original-title=""> <small><span title="">
Senin TAMER</span></small></button>​​​

Why do you want to do this? 为什么要这样做? perhaps $.text() [ Link to doc ] does what you need. 也许$.text() [ 链接到doc ]可以满足您的需求。

Your problem is because replace() is a native JS function, not a jQuery one. 您的问题是因为replace()是本机JS函数,而不是jQuery函数。 So it won't repeat on all buttons. 因此,它不会在所有按钮上重复。 This is another way to do it that is more robust. 这是另一种更健壮的方法。

$('button').each(function(){
    var buttonText = $(this).text();
    $(this).empty().text(buttonText);
});

Don't use regex with HTML. 不要将正则表达式与HTML一起使用。 Seriously, just don't . 认真地说, 就是不要

If you want just the text, use the text() method. 如果只需要文本,请使用text()方法。 From what I understand, this is what you want: 据我了解,这就是您想要的:

$( 'button' ).each( function( index, value ) {
    $( this ).html = $( this ).text()
} )

I wouldn't use html tags inside a button. 我不会在按钮内使用html标签。 If you need to change the button text's appearance, you can use css. 如果需要更改按钮文本的外观,可以使用css。 Then you can grab the button's value using the .html() jquery function. 然后,您可以使用.html()jquery函数获取按钮的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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