动态创建并“单击”与 jQuery 的链接
我想动态创建一个 元素,然后“单击”它。全部无需修改页面。
我正在尝试这个:
$('<a href="mailto:[email protected]"> </a>').click();
...无济于事
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:[email protected]"> </a>').click();
...to no avail
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
它不是jquery,但它工作得很好。
Its not jquery, but it works just fine.
单击链接意味着更改 window.location,那么怎么样?
Clicking on a link means changing window.location, so how about
要使其与 jQuery 一起使用,您首先需要选择 jQuery 对象内的 DOM 元素。
注意 [0]
小提琴:https://jsfiddle.net/fkwhvvhk/
To make it work with jQuery, you first need to select the DOM element inside the jQuery object.
Notice the [0]
fiddle: https://jsfiddle.net/fkwhvvhk/
.click()
使用 DOM,而不是 jQuery 对象,它应该是:
.click()
work with a DOM, not jQuery objectit should be:
尝试这样的事情...
演示:http://jsfiddle.net/wdm954/xtTGX/1
Try something like this...
Demo: http://jsfiddle.net/wdm954/xtTGX/1
您可以这样创建标签:
HTH!
Yo can create the tag this way:
HTH!
为什么不直接将窗口位置更改为链接的 href 呢?您需要使用链接有什么具体原因吗?
否则:
why not just change the window location to the href of the link? Is there any specific reason you need to use a link?
Otherwise:
我发现了一些类似问题的问题,并且找到了对我来说最简单的方法:
就我而言,我失去了很多时间尝试使用 jquery 执行此操作,执行
$('# example').click()
但对我不起作用。至少问题是jquery,我没有它就做到了。我希望它可以对某人有所帮助。这是设置锚点来下载图像并在之后单击的简单方法。I have been found some problems with a similar issue and I found the simplest way for me:
In my case I lost a lot of time trying to do this with jquery, doing
$('#example').click()
but does not work for me. At least the problem was jquery, I did it without it. I hope that it can be help for somenone. Is a simple way to set an anchor to download an image and do click just after.我想说你应该考虑使用 .append() 将 href 添加到容器(主要是 div)并调用 .click()
I would say you should consider adding the href to a container (mostly div) using .append() and call .click()
无法模拟正常点击。您只能触发已绑定到元素的
click
事件处理程序。如 @Alex 已发布, 您可以更改
window.location
来达到相同的效果。It is not possible to simulate normal clicks. You can only trigger
click
event handlers that have been bound to an element..As @Alex has posted, you can change the
window.location
to achieve the same effect..刚刚做了一个关于这个的教程!
这应该选择所有带有“mailto:[email protected]”作为其值。
www.w3schools.com/jquery/jquery_selectors.asp
Just been doing a tutorial on this!
This should select all elements with a href attribute with "mailto:[email protected]" as its value.
www.w3schools.com/jquery/jquery_selectors.asp
您必须使用 .on 然后调用 .click 。
动态生成的超级引用不适用于简单的 .click()
you have to use .on and then call .click .
Dynamically generated hyper reference does not work with simple .click()
如果你想使用 jQuery。这与 jBelanger 的答案基本相同:
问题是$('#link')[ 0] 可能还不存在! 您必须等待它被创建。怎么做呢?我在此处找到了答案。以下对我有用:
If you want to use jQuery. This is basically the same answer as the answer from jBelanger:
The problem is $('#link')[0] might not exist YET! You have to wait for it to be created. How to do that? I found the answer here. The following worked for me: