取得url参数值,并使用jQuery将其添加到图片源 [英] Take url parameter value and add it to image source with jQuery
问题描述
这似乎是一个非常奇怪的问题,但我只是在玩,我想知道它是否有可能.
This is going to seem a very strange question but im just playing about and i'm wondering if its at all possible.
我有一个页面,其中包含所有链接内的图像,我想使用jquery在其父链接的位置中找到"X",然后用该数字替换图像源.
I have a page containing images all within links, I want to use jquery to find 'X' in the location of its parent link and replace the image source with that number.
如果这没有道理,我已经开了一个小提琴来尝试进一步解释.
If this doesnt make sense Ive created a fiddle to try and explain things further.
我知道这是一个奇怪的问题,没有道理,但是可以做到吗?
I know this is a strange question and its not going to make sense, but can it be done?
推荐答案
执行以下操作: http: //jsfiddle.net/qZ5AZ/1/
$("a img").attr("src", function() {
var theSrc = new RegExp("[\\?&]p=([^&#]*)").exec($(this).parent()[0].href)[1];
this.src = theSrc +".jpg";
});
当然,您确实需要返回该值: http://jsfiddle.net/qZ5AZ/9 /
And of course you really need to return the value: http://jsfiddle.net/qZ5AZ/9/
$("a img").attr("src", function() {
var theSrc = new RegExp("[\\?&]p=([^&#]*)").exec($(this).parent()[0].href)[1];
return theSrc +".jpg";
});
查看有关查询字符串的更多信息:
See more about the query string:
这篇关于取得url参数值,并使用jQuery将其添加到图片源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!