Set image in DOM from parameter in URL
I want to make a URL like: www.mysite.com?q=na开发者_如何转开发me
From there, it will place in HTML:
<p align="center"><img src="image/**name**.png" alt="Logo" /></p>
What would be the jQuery attributes to do this? I am still new to jQuery and trying to learn.
assign a Id to your img element and do:
$('#image-id').attr('src', 'image/<name>.png');
Where you replace #image-id with the ID you assigned to your element and has to be extracted from the URL parameters
To extract the from your Querystring you could use this plugin (or - just look at the code and take what you need)
So the complete script would look like this:
$('#image-id').attr('src', 'image/' + $.getURLParam('name') + '.png');
You should assign an id to your img
to identify it:
<p align="center"><img id="logoImg" src="image/**name**.png" alt="Logo" /></p>
Then use a jQuery selector on this id to change the src
attribute based on your URL parameter. The URL String is split on the q= text and the value taken from it.
$('#logoImg').attr('src', 'image/' + document.URL.split('q=')[1] +'.png');
Best way is to just use the plugin mentioned by tigraine, but if you want to parse it yourself, you can access the params via window.location.search
精彩评论