代码之家  ›  专栏  ›  技术社区  ›  Edward Tanguay

如何将jquery代码转换为标准javascript?

  •  0
  • Edward Tanguay  · 技术社区  · 15 年前

    my cell phone apparently doesn't support JQuery ,但运行我所做的简单Javascript测试, ?

    我需要做的就是有基本的 功能。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <script type="text/javascript" src="javascript/jquery-1.4.2.min.js"></script>
            <script type="text/javascript">
                $(document).ready(function() {
                    $("div > div.question").mouseover(function() {
                        if($(this).next().is(':hidden')) {
                            $(this).next().show();
                        } else {
                            $(this).next().hide();
                        }
                    });    
                });        
            </script>
            <style>
                div.flashcard {
                    margin: 0 10px 10px 0;
                }
                div.flashcard div.question {
                    background-color:#ddd;
                    width: 400px;        
                    padding: 5px;    
                    cursor: hand;    
                    cursor: pointer;
                }
                div.flashcard div.answer {
                    background-color:#eee;
                    width: 400px;
                    padding: 5px;    
                    display: none;        
                }
            </style>
        </head>
    <body>
        <div id="1" class="flashcard">
        <div class="question">Who was Wagner?</div>
        <div class="answer">German composer, conductor, theatre director and essayist.</div>
        </div>
    
        <div id="2" class="flashcard">
        <div class="question">Who was Thalberg?</div>
        <div class="answer">a composer and one of the most distinguished virtuoso pianists of the 19th century.</div>
        </div>
    </body>
    </html>
    

    工作!

    谢谢博宾斯!

    alt text
    deviantsart.com )

    3 回复  |  直到 6 年前
        1
  •  0
  •   bobince    15 年前

    window.onload= function() {
        var divs= document.getElementsByTagName('div');
        for (var i= divs.length; i-->0;)
            if (divs[i].className==='question')
                Toggler(divs[i]);
    };
    
    function Toggler(div) {
        var state= false; // assume initially hidden
        var toggled= div.nextSibling;
        while (toggled.nodeType!==1)
            toggled= toggled.nextSibling; // find next element sibling
    
        div.onclick= function() {
            state= !state;
            toggled.style.display= state? 'block' : 'none';
        };
    };
    

    我让它使用 click 而不是在每个 mouseover

    (顺便说一句,避免纯数字 id 属性值。这是无效的,可能会导致奇怪的行为。)

        2
  •  1
  •   Gabriele Petrioli    15 年前

    又一个冗长的回答

    window.onload = function(){
      var questions = getElementsByClass('question',document,'div');
    
      for (var idx=0;idx<questions.length;idx++)
          questions[idx].onclick = function(){
                  var answer = getElementsByClass('answer',this.parentNode,'div')[0];
    
                  if (answer.style.display == '')
                      answer.style.display='block'
                  else
                      answer.style.display = '';
              }
    }
    
    function getElementsByClass(searchClass,node,tag) {
        var classElements = new Array();
        if ( node == null )
            node = document;
        if ( tag == null )
            tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
            if ( pattern.test(els[i].className) ) {
                classElements[j] = els[i];
                j++;
            }
        }
        return classElements;
    }
    

    住在 http://www.jsfiddle.net/WTRFS/1/

        3
  •  0
  •   Nick    15 年前
    <div id="1" class="flashcard" onclick="if (this.style.display == '') this.style.display='none'; else this.style.display = ''">
    card contents
    </div>