Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 将单击功能转换为对象_Jquery_Function_Object_Jquery Isotope - Fatal编程技术网

Jquery 将单击功能转换为对象

Jquery 将单击功能转换为对象,jquery,function,object,jquery-isotope,Jquery,Function,Object,Jquery Isotope,我现在有一个同位素,当我点击一个按钮时,它会显示以下菜单。我有大约二十种选择。我希望将它们更改为对象,这样我就可以重用代码,并使其更易于维护。我对对象编程相当陌生。这是我现在想要更改的代码: $(document).ready(function(){ $("#btn0").click(function(){ $("#difficulty_plan_display").html("<br><h5 id='filter3-checker'>Cars Matchi

我现在有一个同位素,当我点击一个按钮时,它会显示以下菜单。我有大约二十种选择。我希望将它们更改为对象,这样我就可以重用代码,并使其更易于维护。我对对象编程相当陌生。这是我现在想要更改的代码:

$(document).ready(function(){

  $("#btn0").click(function(){
    $("#difficulty_plan_display").html("<br><h5 id='filter3-checker'>Cars Matching your Experience:</h5><button class='button' data-filter-value='*'>show all</button><button class='button' data-filter-value='.model'>Models</button><button class='button is-checked' data-filter-value='.stitch-and-glue'>Stitch and Glue</button><button class='button' data-filter-value='.hybrid'>Hybrids</button><br><br><h5 id='filter4-checker'>Ways to Build:</h5><button class='button' data-filter-value='*'>show all</button><button class='button is-checked' data-filter-value='.kit'>Complete Kits</button><button class='button' data-filter-value='.part'>Wood Parts Only Kit</button><button class='button' data-filter-value='.form'>Forms Kit</button><button class='button' data-filter-value='.plan'>Plans &amp; Manual Only</button><br>");
  });

  $("#btn1").click(function(){
    $("#difficulty_plan_display").html("<br><h5 id='filter3-checker'>Cars matching your Experience:</h5><button class='button' data-filter-value='*'>show all</button><button class='button' data-filter-value='.model'>Models</button><button class='button is-checked' data-filter-value='.stitch-and-glue'>Stitch and Glue</button><br><br><h5 id='filter4-checker'>Ways to Build:</h5><button class='button' data-filter-value='*'>show all</button><button class='button is-checked' data-filter-value='.kit'>Complete Kits</button><br>");
  });

  $("#btn2").click(function(){
    $("#difficulty_plan_display").html("<br><h5 id='filter3-checker'>Cars Matching your Experience:</h5><button class='button' data-filter-value='*'>show all</button><button class='button' data-filter-value='.model'>Models</button><button class='button is-checked' data-filter-value='.stitch-and-glue'>Stitch and Glue</button><br><br><h5 id='filter4-checker'>Ways to Build:</h5><button class='button' data-filter-value='*'>show all</button><button class='button is-checked' data-filter-value='.kit'>Complete Kits</button><button class='button' data-filter-value='.part'>Wood Parts Only Kit</button><br>");
  });

  $("#cartype_btn0").click(function(){
    $("#specific_car_filters_display").html("<br><h3>For All Cars</h3><h5 id='filter5-checker'>Anticipated Use?</h5><button class='button is-checked' data-filter-value='*'>show all</button><button class='button' data-filter-value='.light-touring'>Touring</button><button class='button' data-filter-value='.sailing'>Racing</button><button class='button' data-filter-value='.show'>Showing</button><br>");
  });
  $("#cartype_btn1").click(function(){
    $("#specific_car_filters_display").html("<br><h3>For Convertibles</h3><h5 id='filter5-checker'>Anticipated Use?</h5><button class='button is-checked' data-filter-value='.convertible'>show all</button><button class='button' data-filter-value='.convertible.recreational'>Recreational</button><button class='button' data-filter-value='.convertible.performance'>Performance</button><button class='button' data-filter-value='.convertible.touring'>Touring</button><button class='button' data-filter-value='.convertible.sedan'>Sedan</button><br>");
  });
我在网上查找解决方案,找到了一个参考:

<p><a id="link" href="#">click me</a></p>

<script>
var link = document.getElementById("link");
AttachEvent(link, "click", EventHandler);

function AttachEvent(element, type, handler) {
    if (element.addEventListener) element.addEventListener(type, handler, false);
    else element.attachEvent("on"+type, handler);
}

function EventHandler(e) {
    console.log(this);
}
</script>


All browsers set ‘this’ to the element which fired the event … except one. Internet Explorer 8.0 and below only reference the event handler so ‘this’ is always the global window object.

Fortunately we can determine the target element from the event object instead:


function EventHandler(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
    console.log(target);
}

var link=document.getElementById(“链接”); AttachEvent(链接“单击”,EventHandler); 函数AttachEvent(元素、类型、处理程序){ if(element.addEventListener)element.addEventListener(类型,处理程序,false); else元素.attachEvent(“on”+类型,处理程序); } 函数EventHandler(e){ console.log(this); } 所有浏览器都将“this”设置为触发事件的元素…只有一个除外。Internet Explorer 8.0及以下版本仅引用事件处理程序,因此“this”始终是全局窗口对象。 幸运的是,我们可以通过事件对象确定目标元素: 函数EventHandler(e){ e=e | | window.event; var target=e.target | | e.src元素; 控制台日志(目标); }
但我确实不知道这是否是我所寻找的。如果是,我不知道eventHandler、类型和元素是什么,因为它与我的代码相关

$(“#btn0”)
是一个对象。如果希望在过滤器div中包含一组按钮对象,可以使用

var buttons = document.querySelectorAll(".button-group button");
function buttonHandler(e) {
  e = e || window.event;
  var target = e.target || e.srcElement;
  if(target.id=="btn0") ... // do whatever you want
}

for(var i=0; i<buttons.length; ++i) buttons[i].click = buttonHandler;
要将它们添加到公共单击处理程序,可以使用

var buttons = document.querySelectorAll(".button-group button");
function buttonHandler(e) {
  e = e || window.event;
  var target = e.target || e.srcElement;
  if(target.id=="btn0") ... // do whatever you want
}

for(var i=0; i<buttons.length; ++i) buttons[i].click = buttonHandler;
功能按钮句柄(e){
e=e | | window.event;
var target=e.target | | e.src元素;
如果(target.id==“btn0”)…//做任何你想做的事
}
对于(var i=0;i