我有以下html
<img src="a.jpg" /><p> This is some random text and just to test this example. This is some random text and just to test this example. This is some random text and just to test this example<p>
<br><p>This is some random text and just to test this example This is some random text and just to test this example</p>
<img src="a.jpg" />
<br><p>This is some random text and just to test this example This is some random text and just to test this example</p>
我的问题是,如果我必须只使用jquery来预览文本,假设我在一个名为html的变量中有这个如何只显示文本的几个部分忽略图像
<div id="preview"></div>
$("#preview").val("//display only text in the variable named html")
答案 0 :(得分:2)
你可以从html中filter张图片:
var someHtml = '....';
$('#preview').html($(someHtml).filter(':not("img")'));
答案 1 :(得分:1)
$('#preview').html(textWithTags.replace(/(<([^>]+)>)/ig,"").substr(0, 200));
注意:对于DIV,请使用.text()
或.html()
代替.val()
。
答案 2 :(得分:1)
试试这个:
Here `.content` is class of wrapper of whole html you post
的 CODE:强> 的
$('.preview').click(function(){ // Here `.preview` is the `class` of submit button
html = $('.content').contents(':not(img)');
$('#preview').html(html)
});
或强>
$('#preview').html($(yourhtml).not('img'));
或强>
html = $('.content').html();
yourhtml = $(html).contents(":not('img')").text().substr(0, 200).concat('...'); // here I add `concat('...')` just for continuity. you may remove it,
$('#preview').html(yourhtml);