Using JS to achieve: The dynamic display of page components, hiding

Source: Internet
Author: User
Js| News | show | page

<!--======= demo.html page: ==========-->
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "Qiujy" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
</HEAD>

<BODY>
<TABLE>
&LT;TR id = "Toshowtextarea" style= "Display:block" >
<td><textarea name= "Area" rows= "3" cols= "maxlength=" "style=" Behavior:url (MAXLENGTH.HTC) "onKeyUp=" Javascript:countcharnum (); " ></TEXTAREA></TD>
<td><font color= "Red" > can still enter </font>
<input type= "text" name= "Limit" size= "3" id= "Limit" value= "maxlength=" 3 "readonly=" true "/><font color=" Red "> Chinese characters </font>
</TD>
</TR>
&LT;TR id = "Toshowseletlist" style= "Display:none" >
<td><select name= "Test" >
<option value= "a" >aaaaaaa</option>
<option value= "B" >bbbbbbb</option>
<option value= "C" >ccccccc</option>
<option value= "D" >ddddddd</option>
</SELECT></TD>
<TD> Please select </TD>
</TR>
<TR>
&LT;TD colspan= "2" ><input type= "button" Name= "BUT1" value= "Display text field" >
<input type= "button" Name= "But2" value= "Display drop-down list" ></TD>
</TR>
</TABLE>
</BODY>
</HTML>
<script language= "JavaScript" >
<!--
function Showtextarea () {
toshowtextarea.style.display= "Block";
Toshowseletlist.style.display= "None";
}
function Showselectlist () {
Toshowtextarea.style.display= "None";
toshowseletlist.style.display= "Block";
}

function Countcharnum () {
Limit.value = 50-area.value.length;
}
-->
</SCRIPT>

Maximum text limits for text fields I tried a lot of ways, but only the following is the most effective, (nothing else prevents the user from having to paste the shell)

<!--The following is the HTC used to verify the maximum length limit of the text field: named MAXLENGTH.HTC-->

<public:component id= "Bhvmaxlength" urn= "Maf:maxlength" >
<public:property name= "MaxLength"/>
<public:attach event= "onkeypress" handler= "dokeypress"/>
<public:attach event= "onkeydown" handler= "Dokeydown"/>
<public:attach event= "Onbeforepaste" handler= "Dobeforepaste"/>
<public:attach event= "Onpaste" handler= "Dopaste"/>
<public:attach event= "Oncontentready" handler= "Init"/>

<script language= "JScript" >
JS used to limit the maximum length of a text field
function dokeypress ()
{
if (!isnan (maxLength))
{
MaxLength = parseint (maxLength)
var OTR = Element.document.selection.createRange ()
if (oTR.text.length >= 1)
Event.returnvalue = True
else if (Value.length > MaxLength-1)
Event.returnvalue = False
}
}

function Dokeydown ()
{
settimeout (function ()
{
MaxLength = parseint (maxLength)
if (!isnan (maxLength))
{
if (Element.value.length > MaxLength-1)
{
var OTR = Window.document.selection.createRange ()
Otr.movestart ("character", -1* (Element.value.length-maxlength))
Otr.text = ""
}
}
},1)
}

function Dobeforepaste ()
{
if (!isnan (maxLength))
Event.returnvalue = False
}

function Dopaste ()
{
if (!isnan (maxLength))
{
Event.returnvalue = False
MaxLength = parseint (maxLength)
var OTR = Element.document.selection.createRange ()
var iinsertlength = maxlength-value.length + oTR.text.length
var sData = window.clipboardData.getData ("Text"). substr (0, Iinsertlength)
Otr.text = SData;
}
}

function init ()
{
MaxLength = parseint (maxLength)
if (!isnan (maxLength))
{
Element.value = element.value.substr (0, MaxLength)
}
}
</SCRIPT>

</PUBLIC:COMPONENT>


The following is an effect chart

The effect of clicking "Show drop down list"




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.