javascript - 如何统一功能(将3个功能连接为1个)

标签 javascript jquery html css

我想将 3 个函数合并为 1 个,因为我觉得这是可能的,但我不知道如何正确地实现它

这是我的解决方案。我不明白为什么它只隐藏 location-type2 而不显示它。

我真的不知道这里出了什么问题,因为它正确地隐藏了东西,只是根本不显示它们。

此外,这是我的 HTML。我正在从“数据值”中获取值(value)并使用它来尝试合并这些功能,但它不起作用。

$("#check3, #check2, #check1").click(function () {
    console.log($(this).data("value"));
    if ($(this).data("value") === 1) {

        if ($("#check1").checked) {
            $(".location-type1").removeClass("display-none");
        }
        else {
            $(".location-type1").addClass("display-none");
        }

    }
    else if ($(this).data("value") === 2) {
        if ($("#check2").checked) {
            $(".location-type2").removeClass("display-none");
        }
        else {
            $(".location-type2").addClass("display-none");
        }
    }
    else {
        if ($("#check3").checked) {
            $(".location-type3").removeClass("display-none");
        }
        else {
            $(".location-type3").addClass("display-none");
        }
    }

});

我想合并这 3 个函数:

$("#check1").click(function(){
    if (document.getElementById('check1').checked)
        { 
            $(".location-type1").removeClass("display-none")
        }
    else {
         $(".location-type1").addClass("display-none");
    }
});
$("#check2").click(function(){
    if (document.getElementById('check2').checked)
        {
            $(".location-type2").removeClass("display-none")
        }
    else {

        $(".location-type2").addClass("display-none");
    }
$("#check3").click(function(){
    if (document.getElementById('check3').checked)
        {
            $(".location-type3").removeClass("display-none")
        }
    else {

        $(".location-type3").addClass("display-none");
    }
});      

     <div class="form-group">  <input type="checkbox" style="display: none" id="check1" data-value="1" checked> <label for="check1">Restauracja</label> </div>
     <div class="shop-position location-type1" data-lat="52.4068200" data-lng="16.9299300" data-type="1">
            <div class="location-text">
        <strong>Vininova1</strong>
            <div>Podgórna 14</div>
            <div>Poznań</div>
            <a href="#" class="show-on-map">Pokaż na mapie</a>
            </div>
        </div>



最佳答案

每当您发现自己正在编写与您已经编写的代码相同或非常相似的代码时,您应该停下来思考如何重新编写它以使其更加松散耦合,从而提高可用性。在您的情况下,我们实际上只需要一个包含单个语句的函数。

此外,input 元素已经有一个 value 属性,因此向它们添加一个 data-value 是多余的。

此外,如果可以避免,请不要使用内联样式。它们会导致代码重复并使代码更难阅读和扩展。相反,请使用 CSS 类。

// Get the checkboxes into a JQuery wrapped set
let inputs = $("#parent input[type='checkbox']");

// Set the event handler up on the parent of the 3 checkboxes
$("#parent").on("click", function (event) {
  if(event.target.nodeName === "INPUT"){
    // Get the actual checkbox that was clicked with: event.target 
    // Get the index of the clicked checkbox within the set
    // Toggle the use of the class on the location item that corresponds to the index of the clicked checkbox(es)
    $("#locations div")[$("#parent input[type='checkbox']").index(event.target)].classList.toggle("display-none");
  }
});
.display-none {
  display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="parent">
  <input type="checkbox" value="one">One<br>
  <input type="checkbox" value="two">Two<br>
  <input type="checkbox" value="three">Three
</div>
<div id="locations">
  <div class=".location-type1 display-none">Location 1</div>
  <div class=".location-type2 display-none">Location 2</div>
  <div class=".location-type3 display-none">Location 3</div>  
</div>

关于javascript - 如何统一功能(将3个功能连接为1个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57680720/

相关文章:

javascript - 结构化数据测试工具

javascript - 更改表单提交操作,然后提交表单

php - 选择同一页面的 DIV 内容并将其显示在 PHP 的同一页面上

javascript - -webkit-变换比例: how to get the coordinates of a visible area

javascript - 如何去掉html开头和结尾的&nbsp?

javascript - Node.JS AJAX 空体

javascript - 在加载另一个 View 之前杀死加载的 View

php - html 元素变得奇怪。 (当包含在索引文件中时,按钮元素的尺寸会更改为原始 Bootstrap 组件)

php - 从 PHP 中的货币字符串中获取数值

php - 为什么我的 AJAX 调用在 Internet Explorer 中编辑返回的文本?