如何使用“window.location.href”在 jQuery HTML 插入或换行中?

发布于 2024-09-30 19:19:54 字数 304 浏览 5 评论 0原文

我正在尝试将链接插入到空的 << a>其 ID 为“popup”。该链接将在弹出框中打开当前页面(带有我稍后定义的参数)。我正在尝试将制作超链接所需的 HTML 和 JS 变量“window.location.href”串在一起——如何将它们串在一起?即如何修复此问题以使其正常工作,或者将其重写为以“window.location.href”作为变量的函数:

$("#popup").html('<a href=' . window.location.href . '>open popup</a>');

I'm trying to insert a link into an empty < a > that has the id "popup". The link will open the current page in a popup box (with parameters I'll define later). I'm trying to string together the HTML needed to make a hyperlink plus the JS variable 'window.location.href' -- how can these be strung together? i.e. how do I fix this to make it work, or rewrite it as a function with 'window.location.href' as a variable:

$("#popup").html('<a href=' . window.location.href . '>open popup</a>');

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

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

发布评论

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

评论(4

苏璃陌 2024-10-07 19:19:54

像这样:

$("#popup").html($('<a />', { href: window.location.href, text: 'open popup' }));

或者对于弹出窗口:

$("#popup").html($('<a />', { 
  href: window.location.href, 
  text: 'open popup',
  target: '_blank'
}));

或者,使用 window.open()参数:

$("#popup").html($('<a />', { 
  href: '#', 
  click: function() { window.open(window.location.href, 'popup', 'params'); }
}));

您原来的方法有 2 个问题:

$("#popup").html('<a href=' . window.location.href . '>open popup</a>');
  1. 使用 + 连接字符串
  2. 您需要在属性周围加上引号,

如下所示:

$("#popup").html('<a href="' + window.location.href + '">open popup</a>');

Like this:

$("#popup").html($('<a />', { href: window.location.href, text: 'open popup' }));

Or for the popup:

$("#popup").html($('<a />', { 
  href: window.location.href, 
  text: 'open popup',
  target: '_blank'
}));

Or, using window.open() for parameters:

$("#popup").html($('<a />', { 
  href: '#', 
  click: function() { window.open(window.location.href, 'popup', 'params'); }
}));

There were 2 problems with your original approach:

$("#popup").html('<a href=' . window.location.href . '>open popup</a>');
  1. Use + to concatenate strings
  2. You need quotes around the attribute

Like this:

$("#popup").html('<a href="' + window.location.href + '">open popup</a>');
窗影残 2024-10-07 19:19:54

顺便说一句:在 javascript 中,你用 + 而不是 . 组合字符串(PHP,对吧?):

var something = 'Hello' + ' ' + 'World'; // => 'Hello World'

btw: in javascript you combine strings with a + instead of . (PHP, right?):

var something = 'Hello' + ' ' + 'World'; // => 'Hello World'
满地尘埃落定 2024-10-07 19:19:54
$("#popup").attr("href",window.location.href).attr("target","_blank").text("Open Popup");
$("#popup").attr("href",window.location.href).attr("target","_blank").text("Open Popup");
ペ泪落弦音 2024-10-07 19:19:54

这将更改链接,因此 href 是相同的。

$("#popup").attr('href',window.location.href);

例如

<a id="popup" href="">popup</a>

变成

<a id="popup" href="yourcurrenturl">popup</a>

编辑:

给你一些关于弹出窗口的其他技术的灵感(如果这段代码不起作用,请原谅我,我只是快速地组合在一起)

HTML

 <a href="http://www.developertipoftheday.com" rel="popup" target="alexsite">open alex site in popup</a>

JavaScript

$("a[rel = 'popup']").click(function (event) {

    var popupWindow= window.open($(this).attr("href"), $(this).attr("target"), "status=true,toolbar=false,menubar=false,location=false,width=1018,height=792")

    if (popupWindow=== null) {

        alert("A messasge to inform the user, that the popup has been blocked");
    }

});

This will change the link, so the href is the same.

$("#popup").attr('href',window.location.href);

e.g.

<a id="popup" href="">popup</a>

becomes

<a id="popup" href="yourcurrenturl">popup</a>

EDIT:

To give you a little inspiration for other techniques for popups (forgive me if this if this code doesn't work, I just whipped to together quickly)

HTML

 <a href="http://www.developertipoftheday.com" rel="popup" target="alexsite">open alex site in popup</a>

JavaScript

$("a[rel = 'popup']").click(function (event) {

    var popupWindow= window.open($(this).attr("href"), $(this).attr("target"), "status=true,toolbar=false,menubar=false,location=false,width=1018,height=792")

    if (popupWindow=== null) {

        alert("A messasge to inform the user, that the popup has been blocked");
    }

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