简体   繁体   English

jQuery-$(this)不接受元素

[英]jQuery - $(this) doesn't take the element

So I'm making new project. 所以我正在做新项目。 I have made php multilingual nav-bar and in other language there is no name for one that link. 我已经制作了php multilingual nav-bar,而在其他语言中,没有链接的名称。 I want to hide that link if there is no text but my code doesn't seem to work. 如果没有文本,我想隐藏该链接,但是我的代码似乎不起作用。 Can you help me guys? 你能帮我吗? Thanks! 谢谢!

Here's a link ! 这是一个链接

HTML 的HTML

<div class="navigation">
  <div class="nav_item">
    <a href="#" class="link"><!-- Home --></a>
  </div>
  <div class="nav_item">
    <a href="#" class="link">About</a>
  </div>
  <div class="nav_item">
    <a href="#" class="link">Portfolio</a>
  </div>
</div>

jQuery jQuery的

$(function () {
    if ($("a.link").is(':empty')) {
    $(this).parent().addClass("hidden");
  }
});

For the sake of having another way of doing this, here's my two cents. 为了有另一种方式,这是我的两分钱。

The reason why it is not working is because your $(this) does not refer to your $('a.link') . 它不起作用的原因是因为您的$(this) 没有引用您的$('a.link') I strongly recommend doing some nice console.logs() to try to understand what this is referring to. 我强烈建议做一些很好的console.logs()试图了解this指的是。 It's very important to get a good handle on closure and scopes in JavaScript, since it's what tends to give people headaches if they don't understand it. 掌握JavaScript中的闭包和作用域非常重要,因为如果人们不了解它,这往往会让人头疼。 I've added a working snippet with consoles to help out in understanding. 我添加了一个带有控制台的工作片段,以帮助您理解。

 $(document).ready(function(){ console.log("Who am I?"); console.log(this); $("a.link").each(function(){ console.log("Now who am I?"); console.log(this); // Now this really does refer to your nav links, you should see it printed out in the console if( $(this).is(":empty") ){ console.log(" I am going to be hidden. "); console.log(this); $(this).text("Super hidden"); //You would actually do $(this).parent('.nav-item').hidden() but I did it like this so you can verify visually that it targets the right element } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="navigation"> <div class="nav_item"> <a href="#" class="link"><!-- Home --></a> </div> <div class="nav_item"> <a href="#" class="link">About</a> </div> <div class="nav_item"> <a href="#" class="link">Portfolio</a> </div> </div> 

Explaining each part of the code: 解释代码的每个部分:

$("a.link").each(function(){..});

In this line JQuery is accessing any HTML elements of type a with class link and looping over each one. 在这一行中,JQuery使用类链接访问类型为a的任何HTML元素,并遍历每个元素。 In each loop, it applies the anonymous function (because it has no name or pointer to it) you pass as a parameter to each . 在每个循环中,它应用匿名函数(因为它没有名称或指向它的指针),您将其作为参数传递给each When you are inside the scope of that function, JQuery has kindly binded the this to the element, in this case the a.link . 当您在该函数的作用域内时,JQuery会将该this绑定到该元素,在本例中为a.link You can check this out by looking at the first console.log() of the code, printing out a this that's bound to the window element. 您可以通过查看代码的第一个console.log()并打印出绑定到window元素的this来进行检查。 You will see a whole bunch of information that belongs to that object. 您将看到一大堆属于该对象的信息。 But when you look again at the console and view the console.log() after the text Now who am I? 但是,当您再次查看控制台并在文本之后查看console.log() ,我是谁? you will see the a elements printed out. 您将看到打印出的a元素。 That's the this inside that each() function. 这就是this内部的each()函数。 Cool, right? 酷吧?

if( $(this).is(":empty") ){
   console.log(" I am going to be hidden. ");
   console.log(this);
   $(this).text("Super hidden"); 
}

Inside that anonymous function your this refers to your a.link HTML elements. 内部的匿名函数你this是指您的a.link HTML元素。 You need to do a $(this) in order to make it a JQuery element and use JQuery functions on it. 您需要执行$(this)使其成为JQuery元素并在其上使用JQuery函数。 Once that's done, all you do is ask if it's empty, and if it is then just hide it. 完成后,您要做的就是询问它是否为空,然后将其隐藏。

I know it seems confusing, but it's only at the start. 我知道这似乎令人困惑,但这只是一开始。 Once you understand that JavaScript is all about closures and objects you'll realise how cool it is for it to be the wild wild west. 一旦了解了JavaScript就是关于闭包和对象的全部知识,您就会意识到,成为狂野的西部真是太酷了。 :) :)

That is because context of this refers to window element. 这是因为上下文this是指窗口元素。 You need to use .filter() function here to get element without no text: 您需要在此处使用.filter()函数来获取没有文本的元素:

 $('a.link').filter(function(){
   return !$(this).text().trim();
 }).closest('.nav_item').hide()

 $(document).ready(function() { $('a.link').filter(function(){ return !$(this).text().trim(); }).closest('.nav_item').hide() }); 
 body { padding: 5px; } label { font-weight: bold; } input[type=text] { width: 20em } p { margin: 1em 0 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="navigation"> <div class="nav_item">s <a href="#" class="link"><!-- Home --></a> </div> <div class="nav_item"> <a href="#" class="link">About</a> </div> <div class="nav_item"> <a href="#" class="link">Portfolio</a> </div> </div> 

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

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