preview of text using jquery
I have the following 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>
My question is that if i have to give a preview of the text only using jquery suppose i have this in a variable named html how to display only few parts of the text ignoring the images
<div id="preview"></div>
$("#preview").val("//display only text in the variable named html")
You could filter images from the html:
var someHtml = '....';
$('#preview').html($(someHtml).filter(':not("img")'));
$('#preview').html(textWithTags.replace(/(<([^>]+)>)/ig,"").substr(0, 200));
Note: use .text()
or .html()
instead of .val()
for DIV.
try this:
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)
});
OR
$('#preview').html($(yourhtml).not('img'));
OR
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);
精彩评论