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

时间:2010-07-06 23:44:31

标签: javascript jquery

my cell phone apparently doesn't support JQuery起,但确实运行了我已完成的简单Javascript测试,如何将以下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>

的工作!

谢谢bobince!

alt text http://www.deviantsart.com/upload/o61pba.jpg

3 个答案:

答案 0 :(得分:1)

另一个冗长的答案

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/

答案 1 :(得分:0)

<div id="1" class="flashcard" onclick="if (this.style.display == '') this.style.display='none'; else this.style.display = ''">
card contents
</div>

又快又脏:)

答案 2 :(得分:0)

我没有让NetFront对此进行测试,但是,试图保持它相对不显眼,希望避免破坏浏览器功能:

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属性值。它无效并且可能导致奇怪的行为。)