JS gets the element's tag name implementation Method _javascript skill

Source: Internet
Author: User
Tags tag name tagname

Sometimes we want to get the name of a tag, for example:<div>a</div> get "div". Take a look at the following example:

There is a class= "a" on the page, possibly input, or a normal div or span tag, now we want to add a value to this tag "value1". If it is input, set its Value property to Value1,div to set its innerhtml to value1. How to achieve it ...

If the pure JS is implemented as follows:

Find the code for this tab. This represents the tag object found
if (this.localname== "Input" | | this.localname== "textarea") {
  this.value= "value1"
}else{
  this.innerhtml= "value1";
}
Use jquery to implement the following:
 
if ($ (this) [0].tagname== "Input" | | $ (this) [0].tagname== ' textarea ') {
  $ (this). Val (valueout);
} else{
  $ (this). html (valueout);
But there is a problem is that the name of the label returned by IE is uppercase, so it is safe to write a function that ignores the case for equality of judgment:
 
var equalsignorecase=function (str1, str2) {
  if ( Str1.touppercase () = = Str2.touppercase ()) {return
    true;
  }
  return false;
 
if (Equalsignorecase (this) [0].tagname, "input") | | Equalsignorecase ($ (this) [0].tagname, textarea)] {
  $ (this). Val (valueout);
} else{
  $ (this). html (valueout);

The above is small series for everyone to get the element of JS to achieve the full content of the label name, I hope that we support cloud Habitat Community ~

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.