如何只在按钮点击后才能执行jQuery,而不是在页面加载时执行

时间:2014-02-09 17:11:42

标签: javascript jquery html

我主要尝试了关于这种方式的所有建议,但我无法弄清楚如何在按钮id =“提交”点击上执行javascript效果,而不是在页面加载上。 顺便说一下,这不是我的代码,代码来自http://visualidiot.com/articles/doge

这是Html

<body class="such shibe">
    <img src="images/googletr.png">
    <div class="searchContainer">
        <form>
            <input id="field" name="field" type="text" />
            <button id="submit" href="arama.html" />
        </form>
    </div>


    <script src="//code.jquery.com/jquery-latest.min.js"></script>
    <script src="jquery.doge.js"></script>
    <script>
        $($.doge);
    </script>
</body>

和js文件jquery.doge.js

    (function () {
//  such plugin
$.doge = function(tings) {
    //  very jquery
    var doge = $('body').css('font-family', 'Russo One, sans-serif');

    var r = function(arr) {
        if(!arr) arr = tings; return arr[Math.floor(Math.random() * arr.length)];
    };

    var dogefix = [
        '<img style="height:100px" src="images/img.png">', '<img style="height:150px" src="images/img.png">', '<img style="height:50px" src="images/img.png">',
        '<img style="height:200px" src="images/img.png">', '<img style="height:250px" src="images/img.png">',
        '<img style="height:300px" src="images/img.png">','<img style="height:350px" src="images/img.png">',
    ];

    var very = doge.append('<div class="such overlay" />').children('.such.overlay').css({
        position: 'fixed',
        left: 0,
        right: 0,
        top: 0,
        bottom: 0,
        pointerEvents: 'none'
    });

    setInterval(function() {

        $('.such.overlay').append(
            '<span style="position: absolute; left: ' + Math.random()  *100 + '%;top: ' + Math.random()  *100 + '%;font-size: ' + Math.max(24, (Math.random() * 50 + 100)) + 'px; color: rgb(' + Math.round(Math.random() * 0) + ', ' + Math.round(Math.random() * 0) + ', ' + Math.round(Math.random() * 0) + ');">'
                + r(dogefix) +
            '</span>');        
    }, 50);
};
})(jQuery);;

2 个答案:

答案 0 :(得分:1)

你应该试试这个:

$('#submit').on('click', function(e){
    // Cancel the default event
    e.preventDefault();
    $($.doge);
});

答案 1 :(得分:0)

尝试类似:

$('#submit').on('click', function () {
  // your existing code
});
相关问题