简体   繁体   English

我可以在不使用'this'的情况下重写Javascript吗?

[英]Can I rewrite Javascript without using 'this'?

I'm very new to JS and I'm having trouble getting this to work. 我是JS的新手,我无法让它工作。

Here is my code 这是我的代码

jQuery('ul.menu li').each(function() {
jQuery(this).removeClass('current-menu-item');
jQuery(this).removeClass('current_page_item');
});
jQuery(this).parents('li').addClass('current_page_item');
jQuery(this).parents('li').addClass('current-menu-item');

Now what this should be doing is remove the highlight from one link on a navigation menu, and highlighting the one thats been clicked (I have an AJAX implementation). 现在, 应该做的是从导航菜单上的一个链接中删除突出显示的内容,并突出显示被单击的内容(我有一个AJAX实现)。

For some reason it isn't doing anything. 由于某种原因,它什么也没做。 I have a feeling it is due to 'this' is there another way of structuring this code so I can work out if the code is wrong, which I don't believe it to be, or because of 'this'? 我有一种感觉,这是由于“ this”导致了另一种结构化代码的方式,因此我可以算出代码是否错误(我不认为这是错误的)还是“ this”导致的?

EDIT: 编辑:

Apologies, it seems I haven't given enough information. 抱歉,我没有提供足够的信息。 I'm using the Twenty Fourteen wordpress theme but I'm serving the pages with AJAX. 我正在使用Twenty Fourteen wordpress主题,但我正在使用AJAX服务这些页面。

http://twentyfourteendemo.wordpress.com/ http://twentyfourteendemo.wordpress.com/

I have the code being applied globally (I have other code in the same place to toggle the navigation once clicked (on mobile) and that works fine) 我有全局应用的代码(我在同一个地方有其他代码切换导航一旦点击(在移动设备上),并且工作正常)

I have the menu at the top (without any dropdowns, just links). 我在顶部有菜单(没有任何下拉菜单,只有链接)。 I can't give a link as it's not external currently. 我不能给出一个链接,因为它目前不是外部的。 Should my code be working to change this? 我的代码应该努力改变这个吗?

As a few people have commented "What is 'this'" I feel I've completely missed something. 正如一些人评论“这是什么'这个'”我觉得我完全错过了一些东西。

You don't need loop each item to do a remove class one by one, this is more easy : 你不需要循环每个项目来逐个删除类,这更容易:

jQuery('ul.menu li').removeClass('current-menu-item').removeClass('current_page_item');

Or (it's the same) : 或者(它是一样的):

jQuery('ul.menu li').removeClass('current-menu-item current_page_item');

But I don't understand what is this 'this' : 但我不明白这是什么'这个':

jQuery(this).parents('li').addClass('current_page_item');
jQuery(this).parents('li').addClass('current-menu-item');

Do you mean : 你的意思是 :

jQuery('ul.menu li').addClass('current_page_item current-menu-item');

Or if you are on an event listener (like click, as @Daniel Sanchez feel on comment) you just need to do : 或者,如果您正在使用事件监听器(例如点击,如@Daniel Sanchez对评论的感觉),您只需要:

jQuery('ul.menu li').click(function(){   
  // Remove class on each item  
  jQuery('ul.menu li').removeClass('current-menu-item current_page_item');

  // Add class for this one    
  jQuery(this).addClass('current_page_item current-menu-item'); 
})

It's not entirely clear what you are trying to do but the code can be simplified somewhat: 尚不清楚您要做什么,但是可以对代码进行一些简化:

jQuery("ul.menu li a").click(function(){
    jQuery('ul.menu li').removeClass('current-menu-item current_page_item');
    jQuery(this).parent('li').addClass('current_page_item current-menu-item');
});

http://jsfiddle.net/re3hjzyf/ http://jsfiddle.net/re3hjzyf/

Yes, by replacing this with 'ul.menu li' . 是的,通过更换this'ul.menu li'

So the code would be like this 所以代码就是这样的

jQuery('ul.menu li').each(function() {
   jQuery('ul.menu li').removeClass('current-menu-item')
                       .removeClass('current_page_item');
});
// not sure what the following code is referencing too
// it is outside the bounds of .each() function.
jQuery(this).parents('li').addClass('current_page_item');
jQuery(this).parents('li').addClass('current-menu-item');

what is this 是什么this

When working with JavaScript and many Object Oriented programming languages the this keyword is used to refer to the current context that the programmer is working with. 在使用JavaScript和许多面向对象的编程语言时, this关键字用于引用程序员正在使用的当前上下文。 You're currently referencing to the ul.menu li element so by using this you make a call to the element that is selected in the .each() function. 您当前正在引用ul.menu li元素,因此通过使用this可以调用.each()函数中选定的元素。

You can replace it by using the element selector that you used in the each() function. 您可以使用在each()函数中使用的元素选择器替换它。

jQuery('.current-menu-item').removeClass('current-menu-item');
jQuery('.current_page_item').removeClass('current_page_item');

Here I am making the assumption that only one item will ever have those classes as it would denote which menu item is currently selected. 在这里,我假设只有一个项目将具有这些类,因为它将表示当前选择了哪个菜单项目。 The best way to select it is then to search for the class you want to remove. 最好的选择方法是搜索要删除的类。 (If those classes always go together, you could also remove both on the same line, although then you might want to consider whether you actually need both. (如果这些类总是一起使用,您也可以在同一行上将它们都删除,尽管那样的话,您可能要考虑是否确实需要两者。

jQuery('ul.menu li').on("click", function() {
jQuery(this).addClass('current_page_item').addClass('current-menu-item');
}

You can only use "this" as an argument for the selector when "this" has a value (ie : inside an each loop or inside on. In this case I am using the on() function to apply the function which adds the class to any of the list items which gets clicked on. 当“this”有一个值时,你只能使用“this”作为选择器的参数(即:在每个循环内部或内部开启。在这种情况下,我使用on()函数来应用添加类的函数到点击的任何列表项。

Merging the two you would then end up with : 合并两者,您将最终得到:

jQuery('ul.menu li').on("click", function() {
    jQuery('.current-menu-item').removeClass('current-menu-item');
    jQuery('.current_page_item').removeClass('current_page_item');
    jQuery(this).addClass('current_page_item').addClass('current-menu-item');
}

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

相关问题 如何在不使用JavaScript的情况下使用JQuery库来重写它? - How can I rewrite this without using JQuery library just with JavaScript? 如何将此 jQuery 代码重写为 Javascript? - How can I rewrite this jQuery code into Javascript? 我可以使用Javascript中的数组方法以更好(更短)的条件重写此双循环吗? - Can I rewrite this double loop with condition better (shorter) using array methods in Javascript 如何使用小书签加载javascript文件,而小书签又不会重定向/重写页面? - How can i load a javascript file using a bookmarklet that in turn does not redirect/rewrite the page? 如何在不使用的情况下在 Javascript 中控制音频自动播放<iframe> ? - How can I control audio autoplay in Javascript without using <iframe>? 我可以在不使用 new 关键字的情况下构造 JavaScript 对象吗? - Can I construct a JavaScript object without using the new keyword? 如何在不使用 Active X 的情况下将 DDE 与 Javascript 结合使用? - How can I use DDE with Javascript without using Active X? 如何在不使用FormData的情况下以JavaScript和PHP上传文件 - How can I upload file in JavaScript and PHP without using FormData 如何修改 CSS 标记内容? 不使用 JavaScript? - How can I modify a CSS tag content? Without using JavaScript? 如何在不使用库的情况下反转 JavaScript 中的数组? - How can I reverse an array in JavaScript without using libraries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM