Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的按钮在点击时没有任何作用?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 为什么我的按钮在点击时没有任何作用?

Javascript 为什么我的按钮在点击时没有任何作用?,javascript,jquery,html,Javascript,Jquery,Html,我目前正在尝试建立一个有三个按钮的网站,一个是Facebook共享按钮,一个是Twitter按钮,另一个是视频链接按钮。它们被归类为“facebook”、“twitter”和“play”。但是,它们都没有指向我在javascript文件中编写的链接。这是我的Javascript文件,以防出现问题: $(function() { var clicks = 0; $('button').on('click', function() { clicks++; var percent =

我目前正在尝试建立一个有三个按钮的网站,一个是Facebook共享按钮,一个是Twitter按钮,另一个是视频链接按钮。它们被归类为“facebook”、“twitter”和“play”。但是,它们都没有指向我在javascript文件中编写的链接。这是我的Javascript文件,以防出现问题:

$(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";
});

});
$(函数(){
var=0;
$('button')。在('click',function()上{
点击++;
变量百分比=数学最小值(数学四舍五入(点击次数/3*100),100);
$('.percent').width(percent+'%');
$('.number').text(百分比+'%');
});
$('.facebook')。在('click',function()上{
打开窗户http://www.facebook.com');
var w=580,h=300,
左=(屏幕宽度/2)-(宽/2),
顶部=(屏幕高度/2)-(高度/2);
如果((屏幕宽度<480)| |(屏幕高度<480)){
window.open('http://www.facebook.com/share.php?u=http://bit.ly/somawater“,”,“工具栏=否,位置=否,目录=否,状态=否,菜单栏=否,滚动条=否,可调整大小=否,复制历史=否,宽度=“+w+”,高度=“+h+”,顶部=“+top+”,左侧=“+left”);
}否则{
window.open('http://www.facebook.com/share.php?u=http://bit.ly/somawater“,”,“工具栏=否,位置=否,目录=否,状态=否,菜单栏=否,滚动条=否,可调整大小=否,复制历史=否,宽度=“+w+”,高度=“+h+”,顶部=“+top+”,左侧=“+left”);
}
});
$('.twitter')。在('click',function()上{
var loc=encodeURIComponent('http://bit.ly/somawater'),
title=“由Soma设计的创新型全天然滤水器-”,
w=580,h=300,
左=(屏幕宽度/2)-(宽/2),
顶部=(屏幕高度/2)-(高度/2);
打开窗户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')。在('click',function()上{
window.location.href=”http://kck.st/TH0NAN";
});
});
在我的HTML文件中,我调用这个Javascript文件时说

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

js是javascript的名称。有人能找出问题所在吗?我弄不明白…

如果事件处理程序在呈现按钮之前运行,则按钮无法绑定该处理程序

试着把你的JS代码放在HTML代码之后,我想它会按照你的意愿运行

更新:

试着用这个:

$('html').on( "click", '.facebook', function() {
  // Do what you want
});

当dom就绪时,请检查添加事件的html元素是否在页面上,因为您已经在dom就绪上应用了代码。您可能正在通过其他一些源代码(如ajax)设置html,或者在dom就绪后可能发生的其他代码

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


您的源代码中包含了jQuery?
为什么是我的按钮
…您确定这是您的按钮吗?;)butons在哪里?在jsfiddle.net中实现在浏览器中启动开发工具,看看是否发现错误。可能,我发现元素在JS代码之后异步加载。我想他可能是在那种情况下。
$(window).on('load',function(){

//Your code to be put here
});