如何将JavaScript转换为纯jQuery

如何将JavaScript转换为纯jQuery,javascript,jquery,Javascript,Jquery,我有一些工作JavaScript,我想转换成纯jQuery。请帮帮我。以下是我的完整页面代码: <html> <head> <title>Test</title> <script type="text/javascript"> function Test1(){ w = window.open('test1.html', 'popup1', 'width=800,height=600'); setInterval("Te

我有一些工作JavaScript,我想转换成纯jQuery。请帮帮我。以下是我的完整页面代码:

<html>
<head>
<title>Test</title>
<script type="text/javascript">
function Test1(){
    w = window.open('test1.html', 'popup1', 'width=800,height=600');
    setInterval("Test2()", 5000);
}
function Test2(){
    if(w != null){
        if(w.frames.length == 1){
            return false;
        }else{
            window.open('test2.html', 'popup2', 'width=800,height=600');
        }
    }
}
</script>
</head>
<body>
<a href="#" onclick="Test1();">Test</a>
</body>
</html>

试验
函数Test1(){
w=window.open('test1.html','popup1','width=800,height=600');
setInterval(“Test2()”,5000);
}
函数Test2(){
如果(w!=null){
如果(w.frames.length==1){
返回false;
}否则{
open('test2.html','popup2','width=800,height=600');
}
}
}

谢谢。

我唯一真正想改变的是你的点击处理程序

$('a').click(Test1);
但是,您的主播的
href
属性指向
#
,这是一个坏主意。使其指向有效的资源,如果没有,则使用更合适的元素,如
按钮

我也会改变这一行

setInterval("Test2()", 5000);
…到

setInterval(Test2, 5000);

您不希望或不需要引号,否则JavaScript必须以类似于
eval()

的方式对字符串求值。您唯一可以实际更改的是
单击处理程序

 $('a').click(function() { /* body of Test1 */ });

您可以适当地更新选择器

而不是window.open您可以选择使用类似于的东西,它不需要太多,而jQuery几乎不需要——大多数人宁愿将jQuery转换为“纯Javascript”,而不是相反

  • setInterval(“Test2()”,5000)应该是
    setInterval(Test2,5000)

  • 如果(w!=null)
    -什么是
    w

  • 对于真正的jQuery,将其包装在
    document.ready()处理程序中


  • 
    试验
    $(文档).ready(函数(){
    var w=null;
    函数Test1(){
    w=window.open('test1.html','popup1','width=800,height=600');
    设置间隔(test25000);
    }
    函数Test2(){
    if(w==null | | w.frames.length==1){
    返回;
    }
    open('test2.html','popup2','width=800,height=600');
    }
    //绑定单击处理程序
    $('a')。单击(Test1);
    });
    
    “JavaScript到纯JQuery”意味着JavaScript是JQuery的超集,而不是JQuery的超集。这段代码中没有任何东西可以专门移植到JQuery。这完全没有意义。更好的标题可能是“jQuery如何改进此代码?”为什么?如果你的js工作正常,那很酷。原生js可能比jquery快。我在这里没有看到任何真正需要jquery的东西。你有什么目的吗?另外,使用
    setInterval(test25000)以避免评估。您在哪里定义了
    w
    ?谢谢,Test2函数呢?有机会翻译成Jquery吗?@Anar Jquery是一个JavaScript库。它的代码并不是简单地转换成普通的JavaScript;你可以利用它使事情变得更容易。
    <html>
    <head>
    <title>Test</title>
    <script src="jquery.js"> </script>
    <script>
    $(document).ready(function() {
        var w = null;
    
        function Test1() {
            w = window.open('test1.html', 'popup1', 'width=800,height=600');
            setInterval(Test2, 5000);
        }
    
        function Test2() {
            if (w == null || w.frames.length == 1) {
                return;
            }
            window.open('test2.html', 'popup2', 'width=800,height=600');
        }
    
        // bind the click handler
        $('a').click(Test1);
    });
    </script>
    </head>
    <body>
    <a href="#">Test</a>
    </body>
    </html>