动态创建并“单击”与 jQuery 的链接

发布于 2024-11-17 12:33:20 字数 348 浏览 2 评论 0原文

我想动态创建一个 元素,然后“单击”它。全部无需修改页面。

我正在尝试这个:

$('<a href="mailto:[email protected]">&nbsp;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

顾铮苏瑾 2024-11-24 12:33:20

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

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

Its not jquery, but it works just fine.

var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();    
再见回来 2024-11-24 12:33:20

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

window.location = "mailto:[email protected]";

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

window.location = "mailto:[email protected]";
洛阳烟雨空心柳 2024-11-24 12:33:20

要使其与 jQuery 一起使用,您首先需要选择 jQuery 对象内的 DOM 元素。

$('body').append('<a id="link" href="mailto:[email protected]"> </a>');
$('#link')[0].click();

注意 [0]

小提琴:https://jsfiddle.net/fkwhvvhk/

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

$('body').append('<a id="link" href="mailto:[email protected]"> </a>');
$('#link')[0].click();

Notice the [0]

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

三生路 2024-11-24 12:33:20

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

它应该是:

$('<a href="mailto:[email protected]"></a>')[0].click();

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

it should be:

$('<a href="mailto:[email protected]"></a>')[0].click();
最近可好 2024-11-24 12:33:20

尝试这样的事情...

演示:http://jsfiddle.net/wdm954/xtTGX/1

$('.a').append('<a class="b" href="mailto:[email protected]"> </a>');
$('.b').click(function() {
    window.location = $(this).attr('href');
}).click();

Try something like this...

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

$('.a').append('<a class="b" href="mailto:[email protected]"> </a>');
$('.b').click(function() {
    window.location = $(this).attr('href');
}).click();
我ぃ本無心為│何有愛 2024-11-24 12:33:20

您可以这样创建标签:

$('PARENT_TAG').append('<a id="dinamic_link" href="mailto:[email protected]"> </a>');
//Now click it
$('#dinamic_link').click();

HTH!

Yo can create the tag this way:

$('PARENT_TAG').append('<a id="dinamic_link" href="mailto:[email protected]"> </a>');
//Now click it
$('#dinamic_link').click();

HTH!

叶落知秋 2024-11-24 12:33:20

为什么不直接将窗口位置更改为链接的 href 呢?您需要使用链接有什么具体原因吗?

否则:

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

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:

window.location = 'http://example.com';
我的黑色迷你裙 2024-11-24 12:33:20

我发现了一些类似问题的问题,并且找到了对我来说最简单的方法:

    var link = document.createElement('a');

    link.download = 'profile.png';
    link.href = '...';
    link.id = 'example';
    link.class = '...';

    document.body.appendChild(link);

    link.click();

就我而言,我失去了很多时间尝试使用 jquery 执行此操作,执行 $('# example').click()但对我不起作用。至少问题是jquery,我没有它就做到了。我希望它可以对某人有所帮助。这是设置锚点来下载图像并在之后单击的简单方法。

I have been found some problems with a similar issue and I found the simplest way for me:

    var link = document.createElement('a');

    link.download = 'profile.png';
    link.href = '...';
    link.id = 'example';
    link.class = '...';

    document.body.appendChild(link);

    link.click();

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.

风筝在阴天搁浅。 2024-11-24 12:33:20
$('#something').append('<a id="link" href="mailto:[email protected]"></a>');
$('#link').trigger('click');
$('#something').append('<a id="link" href="mailto:[email protected]"></a>');
$('#link').trigger('click');
你在看孤独的风景 2024-11-24 12:33:20

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

$('parent_div').append('<a id="link" href="mailto:[email protected]"> </a>');
//Now click it
$('#link').click();

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

$('parent_div').append('<a id="link" href="mailto:[email protected]"> </a>');
//Now click it
$('#link').click();
韶华倾负 2024-11-24 12:33:20

无法模拟正常点击。您只能触发已绑定到元素的 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..

五里雾 2024-11-24 12:33:20

刚刚做了一个关于这个的教程!

$("[href='mailto:[email protected]']").click();

这应该选择所有带有“mailto:[email protected]”作为其值。

www.w3schools.com/jquery/jquery_selectors.asp

Just been doing a tutorial on this!

$("[href='mailto:[email protected]']").click();

This should select all elements with a href attribute with "mailto:[email protected]" as its value.

www.w3schools.com/jquery/jquery_selectors.asp

无法回应 2024-11-24 12:33:20

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

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

甩你一脸翔 2024-11-24 12:33:20

如果你想使用 jQuery。这与 jBelanger 的答案基本相同:

$('body').append('<a id="link" href="mailto:[email protected]"> </a>');
$('#link')[0].click();

问题是$('#link')[ 0] 可能还不存在! 您必须等待它被创建。怎么做呢?我在此处找到了答案。以下对我有用:

$('body').append('<a id="link" href="mailto:[email protected]"> </a>');
waitForElm('#link').then((elm) => { elm.click(); });

function waitForElm(selector) {
    return new Promise(resolve => {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector));
        }

        const observer = new MutationObserver(mutations => {
            if (document.querySelector(selector)) {
                resolve(document.querySelector(selector));
                observer.disconnect();
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    });
}

If you want to use jQuery. This is basically the same answer as the answer from jBelanger:

$('body').append('<a id="link" href="mailto:[email protected]"> </a>');
$('#link')[0].click();

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:

$('body').append('<a id="link" href="mailto:[email protected]"> </a>');
waitForElm('#link').then((elm) => { elm.click(); });

function waitForElm(selector) {
    return new Promise(resolve => {
        if (document.querySelector(selector)) {
            return resolve(document.querySelector(selector));
        }

        const observer = new MutationObserver(mutations => {
            if (document.querySelector(selector)) {
                resolve(document.querySelector(selector));
                observer.disconnect();
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    });
}
雨后咖啡店 2024-11-24 12:33:20
var link = document.createElement('<a>')
link.href = "mailto:[email protected]";
link.id = "hitme"
$('#hitme').click();
var link = document.createElement('<a>')
link.href = "mailto:[email protected]";
link.id = "hitme"
$('#hitme').click();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文