简体   繁体   English

Twitter Bootstrap按钮js无法正常工作

[英]Twitter Bootstrap button js not working

I am using Twitter Bootstrap and I can't seem to get the button states to change. 我正在使用Twitter Bootstrap,我似乎无法让按钮状态发生变化。

I am copying the js from http://jsfiddle.net/YCst4/1/ and I'm not getting a response. 我正在从http://jsfiddle.net/YCst4/1/复制js,我没有收到回复。 It's like I don't have jQuery loaded, but I have bootstrap.min.js loaded in my header. 这就像我没有加载jQuery,但我在头文件中加载了bootstrap.min.js。 I don't have any problems with other javascript on the page. 我在页面上的其他JavaScript没有任何问题。

This is what I have in my Codeigniter view: 这就是我在Codeigniter视图中的内容:

<script>
$('#button').button();

$('#button').click(function() {
    $(this).button('loading');
});
</script>

<button class="btn" id="button" type="button" data-text-loading="Loading...">Press Me</button>

Any ideas on what's causing this? 关于是什么导致这个的任何想法? is bootstrap-button.js not a part of the bootstrap.min.js that comes with Bootstrap? 是bootstrap-button.js不是Bootstrap附带的bootstrap.min.js的一部分吗?

http://twitter.github.com/bootstrap/javascript.html#buttons http://twitter.github.com/bootstrap/javascript.html#buttons

EDIT: 编辑:

When I view-source the page, I can see the bootstrap.min.js loaded in the header view, and I can click on it to see it's source, so the path is correct. 当我查看源页面时,我可以看到在头文件视图中加载了bootstrap.min.js,我可以点击它来查看它的源代码,所以路径是正确的。 However, the button js code only works when I repeat <script src="http://site.dev/assets/admin/js/bootstrap.min.js"></script> in the actual content view itself. 但是,按钮js代码仅在我在实际内容视图本身中重复<script src="http://site.dev/assets/admin/js/bootstrap.min.js"></script>时才有效。

I have the jQuery loaded a line above the bootstrap.min.js and the jQuery elements in the same view as the button work perfectly. 我让jQuery在bootstrap.min.js上面加了一行,并且jQuery元素在与按钮完全相同的视图中工作。 I'm stumped. 我很难过。

I had jquery-ui-1.8.21.custom.min.js loading after bootstrap.min.js. 我在bootstrap.min.js之后加载了jquery-ui-1.8.21.custom.min.js。 Apparently there's some conflict. 显然存在一些冲突。

You need to put you code in a document ready function so that it will run after the page is ready, not before. 您需要将代码放入文档就绪函数中,以便在页面准备好之后运行,而不是之前运行。

$(document).ready(function(){
  $('#button').button();

  $('#button').click(function() {
    $(this).button('loading');
  });  
});

JS Fiddle seems to do this automatically for you, which is why it works there. JS Fiddle似乎会自动为您执行此操作,这就是它在那里工作的原因。

You are trying to bind a click to the button before the button is part of the DOM. 您正试图在按钮是DOM的一部分之前将单击绑定到按钮。 Put your stuff in the document ready function and you should be OK. 把你的东西放在文件就绪功能中,你应该没问题。

对于像我这样的javascript新手:你还需要定义$('#button').click() 之前加载jQuery。click $('#button').click()

I had the same problem recently. 我最近遇到了同样的问题。 Here are some things that might help you: 以下是一些可能对您有所帮助的事情:

  1. Are you loading your js files in the necessary order. 您是否按必要的顺序加载js文件? The listed order (as script tags) MUST be in the correct (dependency-based) order like so: 列出的顺序(作为脚本标记)必须是正确的(基于依赖性的)顺序,如下所示:

     <script src="jquery.js"></script> <script src="bootstrap.js"></script> <script src="button_app.js."></script> 
  2. Are you using local or remote css/js files? 您使用的是本地或远程css / js文件吗? If you're using remote ones. 如果你正在使用远程的。 Check if any of them reference github. 检查它们中是否有人引用github。 If they look like this: 如果他们看起来像这样:

     <script src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-alert.js"></script> 

    they won't work. 他们不会工作。 Chrome will throw a mime type error. Chrome会抛出mime类型错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM