简体   繁体   中英

Html tag with id attribute

I was wondering what html tag that suppport id attribute either than <p> tag because i want to change the tag by javascript but i dont want it to be appear in paragraph. This all what ive been trying

<!DOCTYPE html>
<html>
  <body>
    Name : <p id="user">user1</p>
    <script>
      document.getElementById("user").innerHTML="Arvin";
    </script>
  </body>
</html>

but the result is

Name :
Arvin

what I want is

Name : Arvin

thanks for spending your time to read this..any help will much appreciated

Every tag supports id . In your case, <span> would work well.

 document.getElementById("user").innerHTML="Arvin"; 
 Name : <span id="user">user1</span> 

This code goes wrong because paragraph are shown into a new line (by browser). This code put text in two lines (without your Javascript)

<html>
  <body>
    Name : <p id="user">user1</p>
  </body>
</html>

You maybe shoud better do this:

<html>
  <body>
      <p>Name : <span id="user">user1</span></p>
  </body>
</html>

document.getElementById("user").innerHTML="Arvin";

See running example here:

I cannot think of any tag that wouldn't support the id attribute, neither can the HTML5 spec :

3.2.5 Global attributes

The following attributes are common to and may be specified on all HTML elements (even those not defined in this specification): ... id ...

Try adding display: inline style in <p> tag and your problem is solved. You can use id calling on any tag though.

<!DOCTYPE html>
<html>
  <body>
    Name : <p id="user" style="display:inline;">user1</p>
    <script>
      document.getElementById("user").innerHTML="Arvin";
    </script>
  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM