如何将这个特定的jQuery函数转换为JavaScript [英] How to convert this certain jQuery function into JavaScript

查看:60
本文介绍了如何将这个特定的jQuery函数转换为JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



我刚开始学习JavaScript。我目前正在开发一个只接受JavaScript的项目。该项目是切换多个按钮,并为此找到了代码解决方案,但它在jQuery中。它工作得很好......演示是在 http://jsfiddle.net/ladyblue1075/rvhmkpu5/3/ [< a href =http://jsfiddle.net/ladyblue1075/rvhmkpu5/3/target =_ blanktitle =New Window> ^ ]



但我需要用JavaScript而不是jQuery编写的代码函数。任何人都可以帮我如何将这个jQuery函数转换成JavaScript代码?



谢谢!



< pre lang =cs> jQuery(document).ready(function(){

// INT
// 最高的高度
var highestlanguage = 0 ;

// 获得最高语言,因此不会向上滚动。
var _gethighestlanguage = function(){
var _height = jQuery( this )。height() ;
if (highestlanguage< _height){
highestlanguage = _height;
}
};

// 这不是真正的切换。只需关闭所有并向下滑动
var _togglelanguage = function(){
var _target = jQuery( this )。data(' );
jQuery( 。language)。hide();
jQuery( + _ target).slideDown();
};

// 使用language类关闭最近的元素。所以不要再在这些方框中使用它
var _closelanguage = function(){
jQuery( this )。nearest(' 。language')。slideUp( );
};

// 获取最高语言
jQuery( 。language)。each(_gethighestlanguage);

// 为容器指定高度。 (阻止页面在关闭时向上滚动)
jQuery( 。container)。css( min-height,最高语言+ px);

// 全部隐藏
jQuery( 。language)。hide();

// 为按钮分配函数
jQuery(< span class =code-string>'
。toggle')。click(_togglelanguage);
jQuery( 。close)。click(_closelanguage);
});

解决方案

为什么?你真的明白jQuery也是Javascript吗?所以你不能合理地提到只接受JavaScript;这完全无关紧要;在所有情况下,我们讨论Javascript而不是其他。



因此,为什么不把它放在网站上,而不是消除jQuery。这并不意味着安装任何东西。 jQuery只是放在网站上,就像你的Javascript(你在讨论你的问题)一样。



所以,没有转换;听起来就像胡说八道。同样,Javascript而不是jQuery是无稽之谈。请从了解这一点开始。如果,由于一些奇怪的原因,你想要消除jQuery(为什么?!),只是不要使用它(但为什么要问这个问题呢?)。有什么问题?您不知道如何隐藏和显示元素,滑动它等等?然后从文档中学习并询问您的特定问题如果您遇到一些问题(请不要指望有人为您工作;我们通常不会这样做)。只是做这个工作(但为什么要做这个工作,如果它已经在jQuery中为你完成了?!)。



还有什么?你知道吗,如果你没有别的事可做,只需看看jQuery代码。你问题的所有答案都在那里。或者其他一些代码。 :-)



-SA


Hi Experts,

I am just starting to learn JavaScript. And I'm currently working on a project that only JavaScript is accepted. The project is to toggle multiple buttons and have found the code solution for this but it's in jQuery. It's working fine... Demo is at http://jsfiddle.net/ladyblue1075/rvhmkpu5/3/[^]

But I need the code function written in JavaScript instead of jQuery. Can anyone help me how to convert this jQuery function into JavaScript code?

Thank you!

jQuery(document).ready(function(){

    // INT
    // the height of the highest one
    var highestlanguage = 0;

    // get highest language so it will not scroll up.
    var _gethighestlanguage = function () {
        var _height = jQuery(this).height();
        if(highestlanguage<_height) {
            highestlanguage = _height;
        }
    };

    // It's not a "real toggle". Just close all and slide down the one
    var _togglelanguage = function(){
        var _target = jQuery(this).data('target');
        jQuery(".language").hide();
        jQuery("#"+_target).slideDown();
    };

    // close the nearest element with the class "language". So don't use it in these boxes again
    var _closelanguage = function () {
        jQuery(this).closest('.language').slideUp();
    };

    // Get highest language
    jQuery(".language").each(_gethighestlanguage);

    // Assign height to the container. (Prevents page from up-scrolling on close)
    jQuery(".container").css("min-height", highestlanguage + "px");

    // Hide All
    jQuery(".language").hide();

    // Assign functions to the buttons
    jQuery('.toggle').click(_togglelanguage);
    jQuery(".close").click(_closelanguage);
});

解决方案

Why? Do you really understand that jQuery is also Javascript? So you cannot reasonably mention "only JavaScript is accepted"; this is totally irrelevant; in all cases, we discuss Javascript and nothing else.

So, instead of eliminating jQuery, why not, for example, putting it on the site. It would not mean "installing" anything. jQuery is just put on the site in exact same way your Javascript (you are discussing in your question) is put.

So, there is no "conversion"; it sounds just like nonsense. Likewise, "Javascript instead of jQuery" is nonsense. Please start with understanding of this point. If, by some weird reason, you want to eliminate jQuery (why?!), just don't use it (but why asking this question then?). What's the problem? You don't know how to hide and show the element, slide it, and so on? Then learn it from the documentation and ask your particular questions if you face some problems (please don't expect that someone does your work for you; we usually don't do it). Just do the job (but why doing this job if it is already done for you in jQuery?!).

How else? You know what, if you have nothing else to do, just look at jQuery code. All the answers to your questions are there. Or some other code. :-)

—SA


这篇关于如何将这个特定的jQuery函数转换为JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆