我有这段JS:
var count = $(".parent a").length;
$(".parent div").width(function(){
return ($(".parent").width()/count)-5;
}).css("margin-right","5px");
但它似乎在我的网站上不起作用,尽管它在 JSFiddle 上运行良好。
我使用了“http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js”
但没有成功。
我的网站上没有任何其他脚本,因为我刚刚启动它。
这只是使用错误插件的情况吗?
<小时/>更新
这是我的完整代码:
最佳答案
您需要 jQuery,而不是 jQueryUI。
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(function() {
var count = $(".parent a").length;
$(".parent div").width(function(){
return ($(".parent").width()/count)-5;
}).css("margin-right","5px");
});
</script>
只是为了澄清为什么它有效,用 $(function(){...}); 包围你的 jQuery 代码。意味着它只会在页面加载完成后运行。因此,如果您正在处理 html 元素,这会很有用。你会注意到你的 jFiddle JavaScript 是“onLoad”运行的,这就是它工作的原因:)
关于javascript - 不知道使用哪个JQuery插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19512265/