繁体   English   中英

jQuery使两个函数一起运行

[英]Jquery make two functions run together

我有一个搜索按钮,我想在其中单击时同时更改图标以及边框的颜色:

$(function () {
    $("#search").click(function () {
        $("#search").css({ "border": "2px", "border-style": "ridge", "border-color": "#000000" })
    });

    $("#search").click(function () {
        $("#search").replaceWith($('<img>', { src: 'Icons/magnifier.png' }))
    });
});

现在,我的两个函数可以单独工作,但是当我将这两个函数结合在一起时(如我的示例),只有最后一个触发。

有没有一种方法可以组合这两种功能,以便单击时边框和图像都可以改变?

包装盒的CSS:

.topnav img {
    border: 2px ridge #7ab5dc;
    position: relative;
    height: 20px;
    width: 20px;
    float: right;
    margin-top:15px;
    margin-right:20px;
    padding:3px;
    border-radius: 5px;
}

HTML

       <div class="topnav">
            <img id="search" class="search" src="Icons/magnifier2.png" />
        </div>

发生这种情况是因为第二次声明$("#search").click(function () { ...您将覆盖第一个函数。因此,只需将要发生的所有事情都放在同一个.click函数中

每行之后还需要分号。

根据@ jbehrens94关于ID丢失的说明,这是我的最终代码

$("#search").click(function () {
    $(this).replaceWith($('<img id="search">', { src: 'Icons/magnifier.png' })).css({ "border": "2px", "border-style": "ridge", "border-color": "#000000" });
});

1)调用.replaceWith($("<img>"), ...) ,不会将搜索ID保留到图像中。

更新: .replaceWith($("<img id='search'>"), ...)

2)在编辑答案之前,我已经给了您建议。

$(function(){

  var search = $('#search');

  search.click(function(){
    search.replaceWith(...).css(...);
  });

});

这样,您的代码只需进入DOM一次即可获取HTML。 这是一种出色的性能,并且还可以确保您正在使用想要的元素。

3)您还询问了有关淡入/淡出的问题。 您可以使用jQuery的fadeIn(duration, completeCallback)fadeOut(duration, completeCallback)函数。 持续时间以毫秒为单位,回调是一个匿名函数,但您也可以调用另一个函数。

$("#search").fadeIn(400, function(){ alert("Faded in"); });

@Jeppe我刚刚尝试了不同的方法

.topnav img{
    border: 2px ridge #7ab5dc;
    position: relative;
    height: 20px;
    width: 20px;
    float:right;
    margin-top:15px;
    margin-right:20px;
    padding:3px;
    border-radius: 5px;

}

.border-change{
    border: 2px ridge #000000 !important; 

}

您的HTML

<div class="topnav">
            <img id="search" class="search" src="Icons/magnifier2.jpg" />
        </div>

新的单行jQuery

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
$("#search").click(function () {
     $("#search").addClass("border-change").attr("src","Icons/magnifier.jpg");
    });

此线程中有很多帖子,我只想将对我有用的代码发布给可能有相同问题的用户:

    $(function () {
        var search = $("#search");

        search.click(function () {
            search.attr("src", "Icons/magnifier.png").css({ "border": "2px", "border-style": "solid", "border-color": "#808080", "padding-left": "130px", "transition": "all 500ms" });
        });
        $('html').click(function (e) {
            if (e.target.id != 'search') {
                $("#search").attr("src", "Icons/magnifier2.png");
                $("#search").removeAttr('style');;
            }
        });
    })

您可以像这样在单个代码中包含动作

$(“#search”)。click(function(){

    $("#search").css({ "border": "2px", "border-style": "ridge", "border-color": "#000000" }).replaceWith($('<img>', { src: 'Icons/magnifier.png' }));
});

或可能被使用

$(“#search”)。click(function(){

    $("#search").css({ "border": "2px", "border-style": "ridge", "border-color": "#000000" }).html($('<img>', { src: 'Icons/magnifier.png' }));
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM