我有这个代码给我一个关于提交按钮的翻转,我试图让它更通用:
$('.rollover').hover(
function(){ // Change the input image's source when we "roll on"
srcPath = $(this).attr("src");
srcPathOver = ???????
/*need to manipulate srcPath to change from
img/content/go-button.gif
into
img/content/go-button-over.gif
*/
$(this).attr({ src : srcPathOver});
},
function(){ // Change the input image's source back to the default on "roll off"
$(this).attr({ src : srcPath});
}
);
Run Code Online (Sandbox Code Playgroud)
真的有两件事,
我想学习如何操作srcPath变量以将文本'-over'附加到gif文件名,为翻转提供新图像.有谁能建议这样做的方法?
此外,有人可以告诉我这个代码是否可以完善?我对jQuery有点新意,并想知道语法是否可以改进.
非常感谢.
jca*_*ll1 13
$('.rollover').hover(
function(){ // Change the input image's source when we "roll on"
var t = $(this);
t.attr('src',t.attr('src').replace(/([^.]*)\.(.*)/, "$1-over.$2"));
},
function(){
var t= $(this);
t.attr('src',t.attr('src').replace('-over',''));
}
);
Run Code Online (Sandbox Code Playgroud)
要操作文件名并附加"-over",您只需要执行一些Javascript字符串操作,如下所示:
function appendOver(srcPath){
var index = s.indexOf('.');
var before = s.substr(0, index);
var after = s.substr(index);
return before + "-over" + after;
}
Run Code Online (Sandbox Code Playgroud)
这应返回原始文件名(以所有可能的格式)并在扩展点之前添加"-over"字符串.
| 归档时间: |
|
| 查看次数: |
90671 次 |
| 最近记录: |