将HREF放入OnClick活动

发布于 2025-02-11 23:20:34 字数 584 浏览 1 评论 0原文

我有一个页面,其中包含指向多个PDF的不同链接。链接是动态创建的,因此我无法直接编辑HTML。

<a href="https://example.com/presentation-1.pdf" target="_blank">Download</a>
<a href="https://example.com/presentation-2.pdf" target="_blank">Download</a>

我正在尝试打开指向新窗口中PDF的每个链接。 这就是我到目前为止得到的:

$("a[href$='.pdf']").attr("onclick", 'https://example.com','popup','width=600,height=600'); return false;");

当然,新窗口必须包含PDF。因此, https://example.com 代码中应包含正确的PDF的HREF。谁能帮我吗?

I have a page containing different links pointing to several PDF's. The links are dynamically created so I can't edit the HTML directly.

<a href="https://example.com/presentation-1.pdf" target="_blank">Download</a>
<a href="https://example.com/presentation-2.pdf" target="_blank">Download</a>

I'm trying to open every link that points to a pdf in a new window.
This is what I got so far:

$("a[href$='.pdf']").attr("onclick", 'https://example.com','popup','width=600,height=600'); return false;");

Ofcourse the new window has to contain the pdf. So the https://example.com in the code should contain the href of the correct PDF. Can anyone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

黑凤梨 2025-02-18 23:20:34
$(document).on('click', "a[href$='.pdf']", function(e){
    e.preventDefault();
    var href_val = $(this).attr('href');
    window.open(href_val, '', ["width=" + 600,"height=" + 600, 'status=1', 'toolbar=0'].join(','));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="https://example.com/presentation-1.pdf" target="_blank">Download</a>
<a href="https://example.com/presentation-2.pdf" target="_blank">Download</a>

注意 - 新窗口不会在此处打开,因为该请求是在未设置“允许popups”权限的沙盒框架中提出的。

$(document).on('click', "a[href$='.pdf']", function(e){
    e.preventDefault();
    var href_val = $(this).attr('href');
    window.open(href_val, '', ["width=" + 600,"height=" + 600, 'status=1', 'toolbar=0'].join(','));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="https://example.com/presentation-1.pdf" target="_blank">Download</a>
<a href="https://example.com/presentation-2.pdf" target="_blank">Download</a>

Note - New window will not open here because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文