我想将这个jQuery函数命名为“showElements”。我怎么能这样做?
$(function () {
$("#sortable").sortable({
revert: true
});
$("#draggable").draggable({
connectToSortable: "#sortable",
helper: "clone",
revert: "invalid"
});
$("ul, li").disableSelection();
});
答案 0 :(得分:2)
首先,您必须包含jQuery库,然后:
<script type="text/javascript">
function showElements()
{
$("#sortable").sortable({
revert: true
});
$("#draggable").draggable({
connectToSortable: "#sortable",
helper: "clone",
revert: "invalid"
});
$("ul, li").disableSelection();
}
</script>
答案 1 :(得分:2)
在这种情况下,不需要将函数包装在jQuery global($)中。只需创建如下函数:
function showElements() {
$("#sortable").sortable({
revert: true
});
$("#draggable").draggable({
connectToSortable: "#sortable",
helper: "clone",
revert: "invalid"
});
$("ul, li").disableSelection();
}
答案 2 :(得分:1)
类似的东西:
<script type="text/javascript">
var showElements = function () {
$("#sortable").sortable({
revert: true
});
$("#draggable").draggable({
connectToSortable: "#sortable",
helper: "clone",
revert: "invalid"
});
$("ul, li").disableSelection();
};
$(function(){
showElements();
});
</script>
答案 3 :(得分:0)
作为jQuery插件:
(function ($,undefined) {
$.showElements = function () {
$("#sortable").sortable({
revert: true
});
$("#draggable").draggable({
connectToSortable: "#sortable",
helper: "clone",
revert: "invalid"
});
$("ul, li").disableSelection();
};
}(jQuery));
像这样使用它:$.showElements();
仅作为常规功能:
var showElements = function () {
$("#sortable").sortable({
revert: true
});
$("#draggable").draggable({
connectToSortable: "#sortable",
helper: "clone",
revert: "invalid"
});
$("ul, li").disableSelection();
};
像这样使用它:showElements();
脆弱:你正在制定全球范围