English 中文(简体)
Extract all h6 tags from a TextArea using JavaScript
原标题:

Here is what I m looking to do.

1: Have a jQuery WYSIWYG editor that allows users to enter text
2: Have a box that displays extracted text from the WYSIWYG editor that is only viewable. The extracted text should be bulleted. Each bullet item should be anything that is contained in an tag in the WYSIWYG.

Example:

WYSIWYG editor would contain the following text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas ut ipsum eget
enim porttitor pretium. <h6>Ut eget purus.</h6> In nisi congue accumsan. Nulla
mattis nisl at dui porta non lacinia nulla condimentum. <h6>Maecenas convallis
suscipit magna et venenatis.</h6> Phasellus a justo sed mauris hendrerit porttitor.

The view only box below would then show:
- Ut eget purus
- Maecenas convallis suscipit magna et venenatis

Thanks for the help!

最佳回答

Inspiration courtesy of Pekka:

var editorContents = $( <div/> );          // Inject the stuff from the textbox
editorContents.html( $( #editor ).val() ); // into the DOM.

// Adjust to taste -- this is where we re putting the info we extract.
var bulletList = $( <ul/> ).class( whatever ).appendTo( wherever );

// Now just find the headings and put their contents into bullet points.
$( h6 , editorContents).each( function (i) {
  $( <li/> ).text( this.text() ).appendTo(bulletList);
} );

If you re doing this over and over, you re going to want to reuse bulletList, and you re going to want to either reuse editorContents, or else .remove() it each time you re done with it, to keep from leaking DOM objects all over the place :)

Oh, and you might want to use .html() instead of .text() for transferring the contents of the h6 s into the li s. Up to you.

问题回答
var text =  foo <h6>head1</h6> bar <h6>head2</h6> waa ;
var regex = /<h6.*?>(.*?)</h6>/g;
var match = null;
while (match = regex.exec(text)) {
    alert(match[1]); // head1, head2.
}

Use on own risk, do not use regex to parse HTML!

Edit: d oh, I missed the jQuery piece. Go ahead with the proposed jQuery solution, it is much better :)

In jQuery you may select them with

h6s = $( #wysiwyg h6 );

You could create an invisible DIV, put the HTML into the innerHTML property and then query for h6 s in the way that JQuery is so loved for. I don t speak JQuery but it should read something like $("h6") that, of course, requires that your HTML input is valid.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签