使用jQuery动态创建和“单击”链接

时间:2022-11-21 15:41:19

I want to dynamically create an <a href="mailto:..."> element, then "click" it. All without modifying the page.

我想动态创建一个元素,然后在“单击”它。所有这些都没有修改页面。

I'm trying this:

我正在尝试这个:

$('<a href="mailto:test@test.com">&nbsp;</a>').click();

...to no avail

......无济于事

13 个解决方案

#1


14  

Clicking on a link means changing window.location, so how about

点击链接意味着更改window.location,那么怎么样

window.location = "mailto:test@test.com";

#2


32  

Its not jquery, but it works just fine.

它不是jquery,但它工作得很好。

var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();    

#3


10  

To make it work with jQuery, you first need to select the DOM element inside the jQuery object.

要使它与jQuery一起使用,首先需要在jQuery对象中选择DOM元素。

$('body').append('<a id="link" href="mailto:test@test.com">&nbsp;</a>');
$('#link')[0].click();

Notice the [0]

注意[0]

fiddle: https://jsfiddle.net/fkwhvvhk/

#4


4  

Try something like this...

试试这样的事......

Demo: http://jsfiddle.net/wdm954/xtTGX/1

$('.a').append('<a class="b" href="mailto:test@test.com">&nbsp;</a>');
$('.b').click(function() {
    window.location = $(this).attr('href');
}).click();

#5


3  

.click() work with a DOM, not jQuery object

.click()使用DOM,而不是jQuery对象

it should be:

它应该是:

$('<a href="mailto:test@test.com"></a>')[0].click();

#6


2  

Yo can create the tag this way:

你可以用这种方式创建标签:

$('PARENT_TAG').append('<a id="dinamic_link" href="mailto:test@test.com">&nbsp;</a>');
//Now click it
$('#dinamic_link').click();

HTH!

#7


2  

why not just change the window location to the href of the link? Is there any specific reason you need to use a link?

为什么不只是将窗口位置更改为链接的href?您需要使用链接是否有任何特定原因?

Otherwise:

window.location = 'http://example.com';

#8


1  

$('#something').append('<a id="link" href="mailto:test@yourdomain.com"></a>');
$('#link').trigger('click');

#9


1  

I would say you should consider adding the href to a container (mostly div) using .append() and call .click()

我会说你应该考虑使用.append()和调用.click()将href添加到容器(主要是div)

$('parent_div').append('<a id="link" href="mailto:test@test.com">&nbsp;</a>');
//Now click it
$('#link').click();

#10


1  

It is not possible to simulate normal clicks. You can only trigger click event handlers that have been bound to an element..

无法模拟正常点击。您只能触发已绑定到元素的click事件处理程序。

As @Alex has posted, you can change the window.location to achieve the same effect..

正如@Alex发布的那样,你可以改变window.location来达到同样的效果。

#11


1  

Just been doing a tutorial on this!

刚刚做了这个教程!

$("[href='mailto:test@test.com']").click();

This should select all elements with a href attribute with "mailto:test@test.com" as its value.

这应该选择具有href属性的所有元素,并将“mailto:test@test.com”作为其值。

www.w3schools.com/jquery/jquery_selectors.asp

#12


0  

var link = document.createElement('<a>')
link.href = "mailto:test@test.com";
link.id = "hitme"
$('#hitme').click();

#13


0  

you have to use .on and then call .click . Dynamically generated hyper reference does not work with simple .click()

你必须使用.on然后调用.click。动态生成的超级引用不适用于简单的.click()

#1


14  

Clicking on a link means changing window.location, so how about

点击链接意味着更改window.location,那么怎么样

window.location = "mailto:test@test.com";

#2


32  

Its not jquery, but it works just fine.

它不是jquery,但它工作得很好。

var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();    

#3


10  

To make it work with jQuery, you first need to select the DOM element inside the jQuery object.

要使它与jQuery一起使用,首先需要在jQuery对象中选择DOM元素。

$('body').append('<a id="link" href="mailto:test@test.com">&nbsp;</a>');
$('#link')[0].click();

Notice the [0]

注意[0]

fiddle: https://jsfiddle.net/fkwhvvhk/

#4


4  

Try something like this...

试试这样的事......

Demo: http://jsfiddle.net/wdm954/xtTGX/1

$('.a').append('<a class="b" href="mailto:test@test.com">&nbsp;</a>');
$('.b').click(function() {
    window.location = $(this).attr('href');
}).click();

#5


3  

.click() work with a DOM, not jQuery object

.click()使用DOM,而不是jQuery对象

it should be:

它应该是:

$('<a href="mailto:test@test.com"></a>')[0].click();

#6


2  

Yo can create the tag this way:

你可以用这种方式创建标签:

$('PARENT_TAG').append('<a id="dinamic_link" href="mailto:test@test.com">&nbsp;</a>');
//Now click it
$('#dinamic_link').click();

HTH!

#7


2  

why not just change the window location to the href of the link? Is there any specific reason you need to use a link?

为什么不只是将窗口位置更改为链接的href?您需要使用链接是否有任何特定原因?

Otherwise:

window.location = 'http://example.com';

#8


1  

$('#something').append('<a id="link" href="mailto:test@yourdomain.com"></a>');
$('#link').trigger('click');

#9


1  

I would say you should consider adding the href to a container (mostly div) using .append() and call .click()

我会说你应该考虑使用.append()和调用.click()将href添加到容器(主要是div)

$('parent_div').append('<a id="link" href="mailto:test@test.com">&nbsp;</a>');
//Now click it
$('#link').click();

#10


1  

It is not possible to simulate normal clicks. You can only trigger click event handlers that have been bound to an element..

无法模拟正常点击。您只能触发已绑定到元素的click事件处理程序。

As @Alex has posted, you can change the window.location to achieve the same effect..

正如@Alex发布的那样,你可以改变window.location来达到同样的效果。

#11


1  

Just been doing a tutorial on this!

刚刚做了这个教程!

$("[href='mailto:test@test.com']").click();

This should select all elements with a href attribute with "mailto:test@test.com" as its value.

这应该选择具有href属性的所有元素,并将“mailto:test@test.com”作为其值。

www.w3schools.com/jquery/jquery_selectors.asp

#12


0  

var link = document.createElement('<a>')
link.href = "mailto:test@test.com";
link.id = "hitme"
$('#hitme').click();

#13


0  

you have to use .on and then call .click . Dynamically generated hyper reference does not work with simple .click()

你必须使用.on然后调用.click。动态生成的超级引用不适用于简单的.click()