与element.function之类的jquery等效javascript,例如$("div").toggle() [英] jquery equivalent javascript for element.function like $("div").toggle()

查看:73
本文介绍了与element.function之类的jquery等效javascript,例如$("div").toggle()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery,并且想将某些方法转换为本地JavaScript代码.

I'm using jQuery and I want to convert some methods to native JavaScript code.

在jQuery中,我们使用like

In jQuery we are using like

$("div").toggle()

是否可以像这样转换为JavaScript

Is it possible to convert to JavaScript like

document.getElementById("id").callfunction();

推荐答案

是的,您可以这样做.如果您对其他答案感到满意,请说您必须创建新函数,并且必须调用它.然后去吧.

Yes you can do that. If you are satisfied with the other answers, saying that you have to create new function, and you have to call that. Then go for it.

否则,如果您确实要遵循语法,那么它相当简单且可行. (看上去很漂亮的事实)

Otherwise, if you really want to follow the syntax, then it is fairly simple and doable. ( Infact that looks pretty )

这是您可以执行的操作:

This is how you can do it:

Element.prototype.toggle = function() { 

    if ( this.style.display == '' || this.style.display == 'block' ) { 
        alert ( "Changing display to NONE ");
        this.style.display = 'none';
    }else{
        this.style.display = 'block';
        alert ( "Changing display to BLOCK ");
   }
}

记住IE8不支持Element Prototype,但是Chrome,FF和Safari支持.

运行

  1. 在此页面中打开Chrome控制台.
  2. 复制并粘贴上面的代码,然后在其中运行 安慰.
  3. 现在在控制台document.getElementById('question-header').toggle()中执行此操作[这将 在此页面中显示/隐藏问题标题]
  1. Open chrome console in this page.
  2. copy-paste above code, and run in console.
  3. Now execute this in console document.getElementById('question-header').toggle() [ This will show/hide the question title in this page ]

这篇关于与element.function之类的jquery等效javascript,例如$("div").toggle()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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