Nếu bạn đã cài đặt jQuery thì tại sao không làm điều này:
$['#proxyAnchor'][0].click[];
Lưu ý rằng chúng tôi sử dụng [0] để chỉ định phần tử đầu tiên. Bộ chọn jQuery trả về một thể hiện jQuery và calling click [] trên đó chỉ gọi trình xử lý JavaScript, không phải HREF. Gọi nhấp chuột [] trên phần tử thực tế [được trả về bởi [0]] sẽ theo liên kết trong một href, v.v.
Xem ở đây để biết ví dụ để minh họa sự khác biệt: //jsfiddle.net/8hyu9/
Về lý do tại sao mã ban đầu của bạn không hoạt động - có lẽ là do bạn đang gọi title
chứ không phải title[]
. Nếu không có dấu ngoặc đơn JavaScript sẽ trả về bất cứ thứ gì được gán cho thuộc tính title
, không cố gắng thực thi nó.
Hãy thử ví dụ đơn giản sau đây để xem ý tôi là gì:
var f = function[] { return "Hello"; };
alert[f];
alert[f[]];
Đầu tiên sẽ hiển thị văn bản thực tế của hàm, trong khi thứ hai sẽ hiển thị từ "Xin chào" như mong đợi.
Làm cách nào để nhấp vào thẻ neo?
Sử dụng $ ['bộ chọn'] [0], vì $ ['selector'] trả về một đối tượng jQuery, vì vậy $ ['selector']. click [] sẽ kích hoạt trình xử lý nhấp chuột, trong khi $ ['selector'] [0]. click [] sẽ bắn vào nhấp chuột thực tế.
Chúng ta có thể sử dụng thẻ neo trong javascript không?.click[] method to trigger the “click” JavaScript event on an element. This is demonstrated below:
JS
$[document].ready[function[]{[document].ready[function[]{ $['#submit'].click[function[]{$['#submit'].click[function[] { $['#search_link'][0].click[];$['#search_link'][0].click[]; }]}] }];]; |
HTML
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào tôiClick Me & nbsp; & nbsp; & nbsp; & nbsp; nhấp vào neoClick Anchor
|
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào tôi
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào neo
Note that $['selector'][0]
is used, as $['selector']
returns a jQuery object.
Chỉnh sửa trong JSfiddle
Alternatively, if you want to simply navigate to the href location, you can get the href value and assign it to the
window.location object. The URL would then be loaded as if location.assign[]
had been called with the URL.
JS
$[document].ready[function[]{[document].ready[function[]{ $['#submit'].click[function[]{$['#submit'].click[function[] { window.location=$['#search_link'].attr['href'];window.location= $['#search_link'].attr['href']; }]}] }];]; |
HTML
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào tôiClick Me & nbsp; & nbsp; & nbsp; & nbsp; nhấp vào neoClick Anchor
|
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào tôi
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào neo
To open the URL in a new window or tab, you can use the Window interface’s open[] method.
JS
$[document].ready[function[]{[document].ready[function[]{ $['#submit'].click[function[]{$['#submit'].click[function[] { varurl=$['#search_link'].attr['href'];varurl= $['#search_link'].attr['href']; window.open[url];window.open[url]; }]}] }];]; |
HTML
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào tôiClick Me & nbsp; & nbsp; & nbsp; & nbsp; mở trong tab mớiOpen in new tab
|
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào tôi
& nbsp; & nbsp; & nbsp; & nbsp; mở trong tab mới
Chỉnh sửa trong JSfiddle
2. Sử dụng JavaScript
document.getElementById['submit']. title=function[]{.getElementById['submit']. title= function[]{ document.getElementById['search_link'].click[];document.getElementById['search_link'].click[]; Với JavaScript thuần túy, bạn có thể kích hoạt sự kiện JavaScript của Click Click trên một phần tử, như được hiển thị bên dưới: |
HTML
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào tôiClick Me & nbsp; & nbsp; & nbsp; & nbsp; mở trong tab mớiClick Anchor |
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào tôi
& nbsp; & nbsp; & nbsp; & nbsp; mở trong tab mới
Alternatively, to navigate to the href location, you can assign the href value to the location object. To open in a new window or tab, you can use the window.open[]
method.
2. Sử dụng JavaScript
document.getElementById['submit']. title=function[]{.getElementById['submit']. title= function[]{ varurl=document.getElementById['search_link'].href;varurl= document.getElementById['search_link'].href; window.open[url];window.open[url]; Với JavaScript thuần túy, bạn có thể kích hoạt sự kiện JavaScript của Click Click trên một phần tử, như được hiển thị bên dưới: |
HTML
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào tôiClick Me & nbsp; & nbsp; & nbsp; & nbsp; mở trong tab mớiOpen in new tab
|
& nbsp; & nbsp; & nbsp; & nbsp; nhấp vào tôi
& nbsp; & nbsp; & nbsp; & nbsp; mở trong tab mới
Chỉnh sửa trong JSfiddle
2. Sử dụng JavaScript
Với JavaScript thuần túy, bạn có thể kích hoạt sự kiện JavaScript của Click Click trên một phần tử, như được hiển thị bên dưới: :]