Use JavaScript to define and operate custom HTML elements

Source: Internet
Author: User

PreviousArticleDiscussed and usedJavascriptTo manipulateHtmlThe custom attribute of the elementHtmlIn addition toHtmlCan a customHtmlWhat about elements? The answer is yes.

CustomHtmlElements and the system's ownHtmlThe element has the same position and can be usedDocument. getelementbyidThe difference is that the customHtmlElements are invisible, similar to hidden controls.

Use customHtmlElement, we can create a variety of hidden controls, not necessarily limitedInputOfHiddenElement. In addition, it can also be used for some specific needs!

For exampleHtmlDirectly create on the page:

LikeHtmlAdd the element as follows:

VaR Cy = Document. createelement ( " Yyy " );
Cy. ID = " Customcontrol_yyy " ;
Cy. Value = " Custom element yyy " ;
Cy. Checked = True ;
Document. Body. appendchild (CY );

2) UseJavascriptTo operate:

Alert (document. getelementbyid ("customcontrol_yyy"). value );
Alert (document. getelementbyid ("customcontrol_yyy"). Checked );

Here,FirefoxDirect values are supported, but not required.AttributesAttribute Set.

CodeAs follows:

< Html >
< Head >
< Title > Use JavaScript to define and operate custom HTML elements </ Title >
< Script Language = "JavaScript" >
Function Getxxxvalue ()
{
// Alert (document. getelementbyid ("customcontrol_xxx"). Value); // can be used in IE
Alert (document. getelementbyid ( " Customcontrol_xxx " ). Attributes [ " Value " ]. Value ); // IE Firefox can be used
}

// IE Firefox can be used
Function Createyyycontrol ()
{
VaR Cy = Document. createelement ( " Yyy " );
Cy. ID = " Customcontrol_yyy " ;
Cy. Value = " Custom element yyy " ;
Cy. Checked = True ;
Document. Body. appendchild (CY );

Alert (document. getelementbyid ( " Customcontrol_yyy " ). Value );
Alert (document. getelementbyid ( " Customcontrol_yyy " ). Checked );
}
</ Script >
</ Head >
< Body >
< Xxx ID = "Customcontrol_xxx" Value = "Custom element XXX" > </ Xxx >
< Input Type = "Button" Value = "Get xXx element value" Onclick = "Getxxxvalue ();" >
< Input Type = "Button" Value = "Generate the YYY element and obtain the value of this element" Onclick = "Createyyycontrol ();" >
</ Body >
</ Html >


Source code: Http://files.cnblogs.com/redleaf1995/CustomHtmlControl.rar

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.