Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách tìm thuộc tính href và target trong liên kết JavaScript


JavaScript cung cấp document.href và document.target lấy href và target trong liên kết. Liên kết thường đặt trong thẻ <p>, bên trong có thẻ neo chứa href và target. Thảo luận từng cái.

Tìm thuộc tính href

Dùng document.href (thực tế là element.href). Ví dụ href = "https://www.tutorialspoint.com/".

Ví dụ

<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>

Kết quả

tutorialspoint https://www.tutorialspoint.com/


Tìm thuộc tính target

Dùng document.target (element.target). Ví dụ target = "_union".

Ví dụ

<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>

Kết quả

tutorialspoint _union