Customizing HTML Tag Properties

Source: Internet
Author: User

Adding a custom property to an HTML element is convenient, just add it to the angle brackets and be equal to the built-in attribute.

If we want to add property Idvalueto the TextBox element:

<input type= "text" id= "Txtinput" name= "Txtinput" value= "Custom Text" >

Just add: idvalue= "..." after the original control to become:

<input type= "text" id= "Txtinput" name= "Txtinput" value= "Custom Text" idvalue= "Custom Values" >


Idvalue can formally become the Txtinput attribute, and the status is equal to the other attributes.

As the following example, debug through in IE6:

<title> Custom Properties </title>
<script language= "JavaScript" >
function Showtext ()
{
Alert (Document.getelementbyidx ("Txtinput"). Value);
}

function Showvalue ()
{
Alert (Document.getelementbyidx ("Txtinput"). Idvalue);
}

</script>
<body>
<input type= "text" id= "Txtinput" name= "Txtinput" value= "Custom Text" idvalue= "Custom Values" >
<input type= "button" id= "Btnshowtext" name= "Btnshowtext" value= "Display text content" onclick= "Showtext ();" >
<input type= "button" id= "Btnshowvalue" name= "Btnshowvalue" value= "display text value" onclick= "Showvalue ();" >
</body>

But idvalue in Firefox can not pass, mainly because of the strict Firefox control, so these custom properties can not be recognized. After debugging, can only use Document.getelementbyidx ("Txtinput"). attributes["Idvalue"].nodevalue obtained, the method can also be used in IE. So, the same code for IE and Firefox is:

<title> Custom Properties </title>
<script language= "JavaScript" >
function Showtext ()
{
Alert (Document.getelementbyidx ("Txtinput"). Value);
}

function Showvalue ()
{
Alert (Document.getelementbyidx ("Txtinput"). attributes["Idvalue"].nodevalue);
}

</script>
<body>
<input type= "text" id= "Txtinput" name= "Txtinput" value= "Custom Text" idvalue= "Custom Values" >
<input type= "button" id= "Btnshowtext" name= "Btnshowtext" value= "Display text content" onclick= "Showtext ();" >
<input type= "button" id= "Btnshowvalue" name= "Btnshowvalue" value= "display text value" onclick= "Showvalue ();" >
</body>

The following transfers from CSDN

is to write a label that does not have an attribute;
<div textlength= "></div>"
In HTML, the DIV tag does not have a textlength attribute, which I have added myself;

If the page has such an HTML code:
<div> News List
<ul>
<li> [PHOTO] not in line operation obstruction law enforcement officials to enforce the bus to change the operation of the line was investigated [2007-7-31] </li>
<li> Hardware Factory has moved the spectacle factory still disturbing [2007-7-31] </li>
<li> [Photo] "2007 Hong Kong Shopping Festival" is being held-the people go Crazy "blood Fight" [2007-7-27] </li>
<li>[map] The foot of the garden behind, there are a lot of chaos, street law enforcement team show ... [2007-7-27] </li>
</ul>
</div>

I'm going to intercept these characters, of course it's easy; use Getelementsbytabname to remove the list of Li tags and use a for loop to handle each one.

However, how long is the character truncation, naturally to set the variable, where is the variable, if written in JavaScript, this variable is too far away from the HTML code (a head, one in the body), the developer is not easy to operate, because in the page, It can have a lot of such news lists or other lists; so I think to Li's parent label UL, add custom attributes, of course, if it is ol is also possible, as long as it is the parent of Li-line;

<div> News List
<ul textlength= ">"
<li> [PHOTO] not in line operation obstruction law enforcement officials to enforce the bus to change the operation of the line was investigated [2007-7-31] </li>
<li> Hardware Factory has moved the spectacle factory still disturbing [2007-7-31] </li>
<li> [Photo] "2007 Hong Kong Shopping Festival" is being held-the people go Crazy "blood Fight" [2007-7-27] </li>
<li>[map] The foot of the garden behind, there are a lot of chaos, street law enforcement team show ... [2007-7-27] </li>
</ul>
</div>

Compared with the previous, UL more TextLength attribute, is I want to intercept the string length value; then I wrote a function to read this property and to truncate the characters in Li in that area. The functions are as follows:

<script language= "javascript" type= "Text/javascript" >
function Lineshow ()
{
var list = Document.getelementsbytagname_r ("Li");
for (var i = 0; i < list.length; i + +)
{
Gets the custom parameter, which is the character length
var textlength = number (List[i].parentnode.getattribute ("TextLength"));
List[i].setattribute ("title", list[i].innerhtml);
list[i].innerhtml = subString (list[i].innerhtml, textlength);
}
}
function subString (str, len)
{
Len = Len = = NULL | | IsNaN (len)? 0:len;
if (Len < 1 | | str.length <= len) return str;
Return str.substr (0, Len) + "...";
}
</script>

The statements in the above code are more commonly used and do not have to be interpreted too much.

Finally, when the page is loaded, the function is called. The code for calling the function is as follows:
<script language= "javascript" type= "Text/javascript" defer= "true" >
Lineshow ();
</script>

At this point, the effect appears, all the list characters on the page are truncated, when the mouse hit, display its full title;

Customizing HTML Tag Properties

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.