Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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
Javascript 使用“this”关键字作为选择器_Javascript_Jquery - Fatal编程技术网

Javascript 使用“this”关键字作为选择器

Javascript 使用“this”关键字作为选择器,javascript,jquery,Javascript,Jquery,我会使用我事件的选择器来输入我的函数,但它不起作用 我已经尝试了一些技巧,我将在下面的代码中展示 // the actual code $('#voyages').on('click', function() { if (continentOuvert == false) { $('#voyages i.fas.fa-arrow-up').show(500); $('#voyages i.fas.fa-arrow-down').hide(); } }); // the

我会使用我事件的选择器来输入我的函数,但它不起作用

我已经尝试了一些技巧,我将在下面的代码中展示

// the actual code
$('#voyages').on('click', function() {
  if (continentOuvert == false) {
    $('#voyages i.fas.fa-arrow-up').show(500);
    $('#voyages i.fas.fa-arrow-down').hide();
  }
});

// the functions I try to make
function arrowUp() {
  $('this i.fas.fa-arrow-up').show(500);
  $('this i.fas.fa-arrow-down').hide();
}

// or
function arrowUp() {
  $(this + ' i.fas.fa-arrow-up').show(500);
  $(this + ' i.fas.fa-arrow-down').hide();
}
什么都没发生,我想我做得不对

谢谢您的帮助。

您可以使用查找方法:

如前所述,$this不能是选择器,因为它是对象而不是字符串。作为替代方案,您可以使用$e.target,它指向单击的元素,即.voyages,始终传递事件对象。它是演示中的e变量。toggleClass将简化您的代码。而且jQuery是灵活的,所以尽可能不使用id use.class

$'.voyages'。单击,函数E{ var icon=$e.target.find'.fas'; icon.toggleClass'fa-flip-vertical'; }; 钮扣{ 光标:指针; 字号:24px } 航行
谢谢你的回复。我最终使用了以下代码:

函数arrowUpid{ $id+“i.fas.fa向上箭头”。show500; $id+“i.fas.fa向下箭头”。隐藏; }你不应该在arrowUp函数中使用这个+字符串/类名,因为这是一个对象,这样做不会给你任何参考,只会给你一个非常模糊的字符串,比如[object]strstrstrA tip,do console.log$this。在你的代码中找到'i.fas.fa arrow up',在浏览器控制台中选中你得到的元素。这将帮助您理解代码正在做什么/选择替代语法:$i.fas.fa-arrow-up,this.show500;
$(this).find('i.fas.fa-arrow-up').show(500);
$(this).find('i.fas.fa-arrow-down').hide();