为什么单击我的按钮后什么都不做? [英] Why are my buttons not doing anything when clicked on?

查看:69
本文介绍了为什么单击我的按钮后什么都不做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试建立一个具有三个按钮的网站,一个是Facebook共享,一个是Twitter,另一个是链接到视频的按钮.它们被分类为"facebook","twitter"和"play".但是,它们都没有转到我在javascript文件中编写的链接.这是我的Javascript文件,以防万一它有问题:

I am currently trying to build a site that has three buttons, a Facebook Sharing one, a Twitter one, and another one that links to a video. They are classed as "facebook", "twitter", and "play". However, none of them go to the links that I had written in my javascript file. Here is my Javascript file in case something is wrong with it:

$(function() {
var clicks = 0;
$('button').on('click', function() {
    clicks++;
    var percent = Math.min(Math.round(clicks / 3 * 100), 100);
    $('.percent').width(percent + '%');
    $('.number').text(percent + '%');
});


$('.facebook').on('click', function() {
    window.open('http://www.facebook.com');

    var w = 580, h = 300,
            left = (screen.width/2)-(w/2),
            top = (screen.height/2)-(h/2);


        if ((screen.width < 480) || (screen.height < 480)) {
            window.open ('http://www.facebook.com/share.php?u=http://bit.ly/somawater', '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
        } else {
            window.open ('http://www.facebook.com/share.php?u=http://bit.ly/somawater', '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);   
        }

});

$('.twitter').on('click', function() {
    var loc = encodeURIComponent('http://bit.ly/somawater'),
            title = "Beautifully innovative all-natural water filters by Soma — ",
            w = 580, h = 300,
            left = (screen.width/2)-(w/2),
            top = (screen.height/2)-(h/2);

        window.open('http://twitter.com/share?text=' + title + '&url=' + loc, '', 'height=' + h + ', width=' + w + ', top='+top +', left='+ left +', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0');
});

$('.play').on('click', function() {
    window.location.href = "http://kck.st/TH0NAN";
});

});

在我的HTML文件中,我说

In my HTML file, I call on this Javascript file by saying

<script src="/share/share.js"></script>.

Share.js是javascript的名称.有人可以找出问题所在吗?我不知道...

Share.js is what the javascript is named. Can someone spot out what may be the problem? I can't figure it out...

推荐答案

在准备好dom的情况下,请检查页面上是否有添加事件的html元素,因为您已在dom就绪的情况下应用了代码.您可能会通过其他来源(例如ajax)或在dom准备就绪后可能发生的其他事情来设置html.

Please check that the html elements on which you are adding the events are there on the page when dom is ready as you have applied code on dom ready. You might be setting the html through some other source like ajax or something else which might occur after dom ready.

尝试在窗口加载时添加事件:

$(window).on('load',function(){

//Your code to be put here
});

这篇关于为什么单击我的按钮后什么都不做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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