jquery从img src而不是window.location.href获取变量
我发现了这段代码,它的工作原理如下:
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (!results) { return 0; }
return results[1] || 0;}
}
因此,如果 url/query 字符串是 xyz.com/index.html?lang=de
只需调用 var langval = $.urlParam('lang');你已经明白了
——
我的信息来自于点击图像,所以我创建了这段代码:
$('#admin-slideshow img').click(function() {
alert($(this).attr('src'));
}
因此,如果代码是:
<a href="#"><img src="image.php?url=image.jpg&tid=1&opn=1" /></a>
它会发出警报 (image.php?url=image.jpg&tid=1& OPN = 1)。
我的聪明想法是添加代码片段 $(this).attr('src');并将其替换为 window.location.href。这不起作用。有什么建议吗?
$('#admin-slideshow img').click(function() {
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec($(this).attr('src'));
if (!results) { return 0; }
return results[1] || 0;}
}
}
I found this snippet of code, which works a treat:
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^]*)').exec(window.location.href);
if (!results) { return 0; }
return results[1] || 0;}
}
So if the url/query string was xyz.com/index.html?lang=de
just call var langval = $.urlParam('lang'); and you've got it
--
My information is coming from clicking on an image, so I created this code:
$('#admin-slideshow img').click(function() {
alert($(this).attr('src'));
}
So if the code was:
<a href="#"><img src="image.php?url=image.jpg&tid=1&opn=1" /></a>
it would alert just that (image.php?url=image.jpg&tid=1&opn=1).
My brainiac thought was to add that snippet of code $(this).attr('src'); and replace it with the window.location.href. It doesnt work. Any suggestions?
$('#admin-slideshow img').click(function() {
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^]*)').exec($(this).attr('src'));
if (!results) { return 0; }
return results[1] || 0;}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,
用它作为
try this,
use it as
消除 url 参数未找到时的 js 错误。
Removing js errors when url param not found.