[英]Why is my seemingly self-contained web page deaf to jQuery?
I wanted to see if I could take the elements of a jsfiddle and put it into a single page, add the few things necessary, and run it in my browser. 我想看看是否可以将jsfiddle的元素放入单个页面,添加一些必要的东西,然后在浏览器中运行它。
BTW, it works perfectly fine in jsfiddle ( http://jsfiddle.net/clayshannon/QCrXG/2/ ), but not as a "naked" html file. 顺便说一句,它在jsfiddle( http://jsfiddle.net/clayshannon/QCrXG/2/ )中工作得很好,但不能作为“裸”的html文件。
I added, I think, the necessary html (IOW, the stuff that jsfiddle provides behind the scenes), plus the requisite references to the CDN jQuery, jQueryUI js files, and a jQueryUI css file. 我认为,我添加了必要的html(IOW,jsfiddle在幕后提供的东西),以及对CDN jQuery,jQueryUI js文件和jQueryUI css文件的必要引用。
I put the CSS inside the page in a style section. 我将CSS放在页面的样式部分中。
I put the jQuery in a script section (which includes "plugin" code prior to the ready handler). 我将jQuery放在脚本部分(其中在“就绪处理程序”之前包含“插件”代码)。
Here is the entire page: 这是整个页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Platypus Criteria example</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<style>
h1 {
color: chocolate;
font-family:'Segoe UI Light';
}
body {
background-color: oldlace;
}
form {
background-color: antiquewhite;
}
legend {
font-family:'Century Gothic', Verdana, Georgia, sans-serif;
font-size: 1.2em;
color: navy;
}
.staticLabel {
display: inline-block;
width: 140px;
margin-right: 2px;
text-align: right;
font-family: Consolas, Candara, sans-serif;
}
.checkboxGroups1Col { width: 200px; margin-bottom: 10px; border: 1px solid chocolate; }
.checkboxGroups2Col { width: 400px; margin-bottom: 10px; border: 1px solid chocolate; }
.checkboxGroups3Col { width: 600px; margin-bottom: 10px; border: 1px solid chocolate; }
.checkboxGroups4Col { width: 800px; margin-bottom: 10px; border: 1px solid chocolate; }
.checkboxGroups5Col { width: 1000px; margin-bottom: 10px; border: 1px solid chocolate; }
label { display: inline-block; width: 200px; }
#dbills, #WebbedFeet {
min-height: 200px;
}
.submitButton {
background-color: SkyBlue;
color: white;
font-size: 1.2em;
margin-top: 15px;
}
</style>
<body>
<h1>Platypus Criteria</h1>
<form>
<fieldset>
<legend>Date Range</legend>
<label for="BeginDate" class="staticLabel">Begin Date</label>
<input id="BeginDate" />
<label for="BeginTime" class="staticLabel">Begin Time</label>
<input id="BeginTime" value="00:00:00" />
<br/>
<label for="EndDate" class="staticLabel">End Date</label>
<input id="EndDate" />
<label for="EndTime" class="staticLabel">End Time</label>
<input id="EndTime" value="23:59:59" />
</fieldset>
<br/>
<label for="UPC" class="staticLabel">UPC Starts with</label>
<input id="UPC" />
<br/>
<br/>
<div id="accordion">
<h3 id="deptHeader">Duckbills (selected: <span>all</span>)</h3>
<div id="dbills" class="checkboxGroups4Col">
</div>
<h3 id="WebbedFeetHeader">WebbedFeet (selected: <span>all</span>)</h3>
<div id="WebbedFeet" class="checkboxGroups4Col">
</div>
</div>
<br/>
<input type="button" class="submitButton" value="Determine dbills and WebbedFeet selected" id="submitButton" />
</form>
</body>
<script type="text/javascript">
(function ($) {
$.fn.extend({
appendAllCheck: function () {
var selectors = $('<div class="selectors"></div>').appendTo(this)
.append('<button>Select all</button><button>Deselect all</button>');
selectors.find('button').click(function () {
var checked = $(this).text() == 'Select all';
$(this).closest('.selectors').parent()
.find('[name]:checkbox').prop('checked', checked);
$(this).prop('checked', false);
return false;
});
return this;
},
appendCheckboxes: function (name, labels, props) {
var container = this;
$.each(labels, function (i, l) {
var label = $.isPlainObject(labels) ? i : l,
value = $.isPlainObject(labels) ? l : i;
$('<label>').append(
$('<input>', {
'type': 'checkbox',
'name': name
}).val(value).prop(props||{}),
label).appendTo(container);
});
return this;
},
checkedBoxes: function () {
return this.find(':checked').map(function () {
return $(this).parent().text();
}).get();
}
});
})(jQuery);
$(function() {
var dbillsArray = [];
for (var i = 2; i < 100; i++) {
dbillsArray.push(i);
}
var WebbedFeetArray = [];
for (var i = 1; i < 7; i++) {
WebbedFeetArray.push(i);
}
$('#BeginDate, #EndDate').datepicker();
$('#accordion').accordion();
$('button').button();
$('#dbills').appendAllCheck().appendCheckboxes('dbillsCheckboxList', dbillsArray, {
checked: true
});
$('#WebbedFeet').appendAllCheck().appendCheckboxes('WebbedFeetCheckboxList', WebbedFeetArray, {
checked: true
});
$('#submitButton').click(function() {
dbillsList = $('#dbills').checkedBoxes();
$('#deptHeader span').html(dbillsList.join(", "));
WebbedFeetList = $('#WebbedFeet').checkedBoxes();
$('#WebbedFeetHeader span').html( WebbedFeetList.join(", "));
return false;
});
});
</script>
</html>
Is it possible for this to work? 这可能可行吗? If so, what have I omitted or blunderbusted?
如果是这样,我遗漏了什么?
Also: why is the page so pison slow to load? 另外:为什么页面如此缓慢地加载页面?
This way of working seems handy for small "applets" or utilities ('quick-and-dirty' type things), that can be run without having to deploy to a server; 这种工作方式似乎适用于小型“小应用程序”或实用程序(“快捷方式”之类的东西),它们可以运行而无需部署到服务器。 even if you need to use a .js file that is not hosted on a CDN, you can simply save a local copy of it and reference it like so:
即使您需要使用CDN上未托管的.js文件,也可以简单地保存该文件的本地副本并引用它,如下所示:
<script src="checkboxplugin.js"></script>
(this example assumes the .js file and the .html file are living side-by-side as "siblings" in the same folder). (此示例假定.js文件和.html文件作为“兄弟姐妹”并排存在于同一文件夹中)。
There's a few things. 有几件事。
If you're not using a webserver but opening the file directly in the browser you'll need to change: 如果您不是使用网络服务器,而是直接在浏览器中打开文件,则需要进行以下更改:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
to 至
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
And you should add the script tags at the bottom of the file BEFORE the closing </body>
tag, not after it, and the style tags should be inside the <head>
tags, ie before the closing </head>
tag. 你应该在文件的底部添加脚本标记结束前
</body>
标签,以后不是和风格的标签应该是里面<head>
标签,即在收盘前</head>
标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.