Use JavaScript to create powerful GUIs (2)

Source: Internet
Author: User
Create we first create a simple toolbar with three buttons: a bold button, a Italic button, and a connection button. The toolbar is a great way to add functionality to an existing text field, allowing users to simply control the text you enter without having to know HTML. Any Web site that allows users to participate or make feedback can be enhanced with this toolbar.

Our toolbars can be functionally divided into the following 4 sections:
• JavaScript functions that encapsulate HTML markup for selected text attachments
• Style sheet for customizing the appearance and style of toolbars, buttons
• JavaScript functions that respond to mouse events
• HTML that contains toolbar code, images, and table elements

Let's first look at two functions that handle inserting HTML code into <textarea>:

Using JavaScript to work with text sets

function Format_sel (v) {
var str = Document.selection.createRange (). text;
Document.my_form.my_textarea.focus ();
var sel = Document.selection.createRange ();
Sel.text = "<" + V + ">" + str + "<" + V + ">";
Return
}
Format_sel () accepts only one argument, a string that represents the HTML tag that is acting on the selected text. In this toolbar, we use this function to control the text between <b> and <i>. Of course, if you prefer, we can use <strong> and <em> replace <b> and <i>, or use this function to control the selected text, or to qualify the specified text in the selected tag.

We can use the Createrange () method of the Selection object to easily create the TextRange object for the current text. By accessing its Text property, we can get the text selected in <textarea>. The Text property is assigned to a local variable. In the next line, we call focus () on <textarea>, which is very important, otherwise our changes to the text may be written to other parts of the page. Finally, we create another reference to the specified text and assign it a new value: The address of the original selection located in the appropriate HTML tag.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.