使用jquery更改hover的src

发布于 2024-12-11 03:34:20 字数 2940 浏览 0 评论 0原文

我需要关闭默认悬停状态。我有以下代码。

$(this).attr("id"); 
if ((this.id == "defaultTab") ){
$(img#defaultTab)[0].src.replace("_on","_off");
},

我只是告诉代码,如果“this”悬停的 id 为 DefaultTab,则获取 defaultTabs 图像 src 并替换它。

这让我又犯了错误。

请帮忙。

谢谢,


我将发布我的整个功能以供审查。发生的情况是“defaultTab”似乎不作为我从中获取 src 的对象存在。

// **************  TABs  ********************//

jQuery.preloadImages = function()
{
for(var i = 0; i<arguments.length; i++)
    {
        jQuery("<img>").attr("src", arguments[i]);
    }
} 

// preload images first (can run before page is fully loaded)
$.preloadImages("images/tabs01_off.jpg", "images/tabs01_on.jpg",   "images/tabs02_off.jpg","images/tabs02_on.jpg","images/tabs03_off.jpg","images/tabs03_on.jpg","images/tabs04_off.jpg","images/tabs04_on.jpg","images/tabs05_off.jpg","images/tabs05_on.jpg","images/tabs06_off.jpg","images/tabs06_on.jpg","images/tabs07_off.jpg","images/tabs07_on.jpg","images/17.jpg","images/22.jpg","images/24.jpg","images/28.jpg","images/30.jpg","images/31.jpg","images/38.jpg" );
$(
    function()
        {
            // set up rollover  -- this controls the hover states
            $("img.rollover").hover(
                function()
                    {

                    var image_id=$(this).attr("data-image"); // created a variable, making this an Jquery wrapped object.
                    this.src = this.src.replace("_off","_on");
                    $('#changeImg').css("background-image", "url(images/"+ image_id +'.jpg)'); 
                    $("#default_img").hide();


                    $(this).attr("id"); 
                    if (!(this.id == "defaultTab") ){
                        document.getElementById("defaultTab");
                        console.log();
                     $(this.id)[0].src.replace("_on","_off");
                      console.log('img.defaultTab');
                    }

                },
            function()
                {
                    this.src = this.src.replace("_on","_off");
                }
        );
    }

我的 html 片段是:

<tr>
<td>&nbsp;</td>
<td width="629"><img src="images/tabs01_on.jpg" class="rollover" data-image="28" width="89" height="55" id="defaultTab" /><img src="images/tabs02_off.jpg" class="rollover" data-image="24" width="91" height="55" /><img src="images/tabs03_off.jpg" class="rollover" data-image="30" width="90" height="55" /><img src="images/tabs04_off.jpg" class="rollover" data-image="22" width="89" height="55" /><img src="images/tabs05_off.jpg" class="rollover" data-image="17" width="91" height="55" /><img src="images/tabs06_off.jpg" class="rollover" data-image="38"  width="90" height="55" /><img src="images/tabs07_off.jpg" class="rollover" data-image="31" width="90" height="55" /></td>
</tr>

I need to turn off a default hover state. I have the following code.

$(this).attr("id"); 
if ((this.id == "defaultTab") ){
$(img#defaultTab)[0].src.replace("_on","_off");
},

All I am telling the code if "this" hover has an id of DefaultTab, then take defaultTabs image src and replace it.

This is throwing me back errors.

Please help.

Thanks


I am posting my entire function for review. What is happening is that "defaultTab" doesn't seem to exist as an object for me to get the src from.

// **************  TABs  ********************//

jQuery.preloadImages = function()
{
for(var i = 0; i<arguments.length; i++)
    {
        jQuery("<img>").attr("src", arguments[i]);
    }
} 

// preload images first (can run before page is fully loaded)
$.preloadImages("images/tabs01_off.jpg", "images/tabs01_on.jpg",   "images/tabs02_off.jpg","images/tabs02_on.jpg","images/tabs03_off.jpg","images/tabs03_on.jpg","images/tabs04_off.jpg","images/tabs04_on.jpg","images/tabs05_off.jpg","images/tabs05_on.jpg","images/tabs06_off.jpg","images/tabs06_on.jpg","images/tabs07_off.jpg","images/tabs07_on.jpg","images/17.jpg","images/22.jpg","images/24.jpg","images/28.jpg","images/30.jpg","images/31.jpg","images/38.jpg" );
$(
    function()
        {
            // set up rollover  -- this controls the hover states
            $("img.rollover").hover(
                function()
                    {

                    var image_id=$(this).attr("data-image"); // created a variable, making this an Jquery wrapped object.
                    this.src = this.src.replace("_off","_on");
                    $('#changeImg').css("background-image", "url(images/"+ image_id +'.jpg)'); 
                    $("#default_img").hide();


                    $(this).attr("id"); 
                    if (!(this.id == "defaultTab") ){
                        document.getElementById("defaultTab");
                        console.log();
                     $(this.id)[0].src.replace("_on","_off");
                      console.log('img.defaultTab');
                    }

                },
            function()
                {
                    this.src = this.src.replace("_on","_off");
                }
        );
    }

)

My html piece is:

<tr>
<td> </td>
<td width="629"><img src="images/tabs01_on.jpg" class="rollover" data-image="28" width="89" height="55" id="defaultTab" /><img src="images/tabs02_off.jpg" class="rollover" data-image="24" width="91" height="55" /><img src="images/tabs03_off.jpg" class="rollover" data-image="30" width="90" height="55" /><img src="images/tabs04_off.jpg" class="rollover" data-image="22" width="89" height="55" /><img src="images/tabs05_off.jpg" class="rollover" data-image="17" width="91" height="55" /><img src="images/tabs06_off.jpg" class="rollover" data-image="38"  width="90" height="55" /><img src="images/tabs07_off.jpg" class="rollover" data-image="31" width="90" height="55" /></td>
</tr>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别把无礼当个性 2024-12-18 03:34:20

当您悬停时,下面将用 _on 替换 _off ,当您不悬停时,将 _on 替换为 _off 。

如果您将鼠标悬停在另一个选项卡上,请关闭默认设置;如果您没有悬停,则默认设置将打开。

$('.rollover').not('#defaultTab').hover(function(){
    $(this).attr('src',$(this).attr('src').replace("_off","_on"));
    $('#defaultTab').attr('src',$('#defaultTab').attr('src').replace("_on","_off"));
},function(){
    $(this).attr('src',$(this).attr('src').replace("_on","_off"));
    $('#defaultTab').attr('src',$('#defaultTab').attr('src').replace("_off","_on"));
});

如果您希望某人悬停的最后一个项目保持“打开”而不是默认的“打开”,那么您可以使用以下命令:

$('.rollover').hover(function(){
    var hovered = $(this); // save the element we hovered on to use below.
    $('.rollover').each(function(){  
        var tab = $(this); // $(this) in here is each tab as we go through all of them, not the one we hovered on
        tab.attr('src',tab.attr('src').replace("_on","_off")); // Turn off ALL tabs.
    }
    hovered.attr('src',hovered.attr('src').replace("_off","_on")); // Turn on the tab we hovered on
},function(){ return false; });

The below will replace _off with _on when you are hovering, and replace _on with _off when you are not hovering.

Turn off default if you hover on another tab, if you are not hovering, then default will be on.

$('.rollover').not('#defaultTab').hover(function(){
    $(this).attr('src',$(this).attr('src').replace("_off","_on"));
    $('#defaultTab').attr('src',$('#defaultTab').attr('src').replace("_on","_off"));
},function(){
    $(this).attr('src',$(this).attr('src').replace("_on","_off"));
    $('#defaultTab').attr('src',$('#defaultTab').attr('src').replace("_off","_on"));
});

If you want the last item someone hovered on to stay 'on' instead of the default being 'on', then you can use this instead:

$('.rollover').hover(function(){
    var hovered = $(this); // save the element we hovered on to use below.
    $('.rollover').each(function(){  
        var tab = $(this); // $(this) in here is each tab as we go through all of them, not the one we hovered on
        tab.attr('src',tab.attr('src').replace("_on","_off")); // Turn off ALL tabs.
    }
    hovered.attr('src',hovered.attr('src').replace("_off","_on")); // Turn on the tab we hovered on
},function(){ return false; });
末蓝 2024-12-18 03:34:20

您不能使用 .src 您需要使用 .attr('src',new_href)

you cant use .src you need to use .attr('src',new_href)

懷念過去 2024-12-18 03:34:20

更新:现在我完全理解了问题,这是我的建议:

您可以搜索该图像,该图像的 src 属性中包含 _on (请参阅属性包含选择器)并更改值。例如(仅包括相关代码):

function toggle(element, on) {
    var from = on ? '_off' : '_on',
        to = on ? '_on' : '_off';

    element.attr('src', function(i, src) {
        return src.replace(from, to);
    });
}


$("img.rollover").hover(function(){
    toggle($('img.rollover[src*="_on"]'), false);
    toggle($(this), true);
}, function() {
    toggle($(this), false);
});

那么你甚至不需要用ID来标记默认图像。


我不太清楚您的意思是采用defaultTabs图像。如果 this 引用图像,您可以简单地执行以下操作:

if (this.id === "defaultTab"){
    this.src = this.src.replace("_on","_off");
}

如果另一方面,图像是 this 的后代,则必须使用 find:

if (this.id == "defaultTab"){
    $(this).find('img').attr(src, function(i, value) {
        return value.replace("_on","_off");
    });
}

注意,同一个ID只能分配给一个元素,不能分配给多个元素。

Update: Now that I completely understood the problem, here is my suggestion:

You can search for the image, which contains _on in its src attribute (see the Attribute Contains Selector) and change the value. E.g. (only including relevant code):

function toggle(element, on) {
    var from = on ? '_off' : '_on',
        to = on ? '_on' : '_off';

    element.attr('src', function(i, src) {
        return src.replace(from, to);
    });
}


$("img.rollover").hover(function(){
    toggle($('img.rollover[src*="_on"]'), false);
    toggle($(this), true);
}, function() {
    toggle($(this), false);
});

Then you don't even need to mark the default image with an ID.


I am not quite sure what you mean with take defaultTabs image. If this refers to the image, you can simple do:

if (this.id === "defaultTab"){
    this.src = this.src.replace("_on","_off");
}

If on the other hand, the image is a descendant of this, you have to use find:

if (this.id == "defaultTab"){
    $(this).find('img').attr(src, function(i, value) {
        return value.replace("_on","_off");
    });
}

Note that the same ID can only be assigned to one element, not to multiple ones.

吃→可爱长大的 2024-12-18 03:34:20

我很难理解你的逻辑,但也许是这样的:

$(this).mouseenter(function() {
    if (this.id != 'defaultTab') {
        return;
    }
    this.src = this.src.replace(/_on/g,'_off');
});

I have a hard time understanding your logic, but perhaps something like:

$(this).mouseenter(function() {
    if (this.id != 'defaultTab') {
        return;
    }
    this.src = this.src.replace(/_on/g,'_off');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文