开发者

Add variable to jQuery src attribute

开发者 https://www.devze.com 2022-12-14 06:01 出处:网络
I have a script that I reuse in lots of different places. The script has several image paths specified, and I want to make it easier to implement by sett开发者_如何转开发ing one \'path\' variable at t

I have a script that I reuse in lots of different places. The script has several image paths specified, and I want to make it easier to implement by sett开发者_如何转开发ing one 'path' variable at the start then calling the variable in place of the full url. e.g.

jQuery(document).ready(function($){

var path = "http://www.mydomain.com/images/";

$(".menu img.arrow").click(function(){ 

 $(this).attr('src', path + 'arrow_hover.png'); 

    })                              

});

Any pointers?


You just want to have a globally-accessible variable that holds your base path? Simply moving

var path = "http://www.mydomain.com/images/";

outside your function will do that. I often create a "settings" object that holds all of that sort of stuff, so I don't use too many global variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消