Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีหา href และ target attributes ในลิงก์ด้วย JavaScript

เข้าถึงผ่าน element ได้โดย element.href และ element.target

หา href attribute

<html> <body> 
<p><a id="myId" href="https://www.tutorialspoint.com/">tutorialspoint</a></p> 
<script> 
    var ref = document.getElementById("myId").href; 
    document.write(ref); 
</script> </body> </html>

ผลลัพธ์

https://www.tutorialspoint.com/

หา target attribute

<html> <body> 
<p><a id="myId" target="_union" href="https://www.tutorialspoint.com/">tutorialspoint</a></p> 
<script> 
    var ref = document.getElementById("myId").target; 
    document.write(ref); 
</script> </body> </html>

ผลลัพธ์

_union