是否可以在$(document).ready();
功能中选择要定位的页面?
类似
$('page1.html').ready();
提前致谢!
答案 0 :(得分:1)
这些是jQuery(又名jQuery DOM Ready)中常用的不同类型的Document Ready函数。许多开发人员似乎在不知道原因的情况下使用它们。所以我将尝试解释为什么你可以选择一个版本而不是另一个版本。将文档就绪函数看作是一个自动执行的函数,它在页面元素加载后触发。
有关如何使用文档就绪函数的详细信息,请参阅在何处声明您的jQuery函数。
文件就绪示例1
$(document).ready(function() {
//do jQuery stuff when DOM is ready
});
文档就绪示例2
$(function(){
//jQuery code here
});
这相当于例1 ......它们的字面意思相同。
文件就绪示例3
jQuery(document).ready(function($) {
//do jQuery stuff when DOM is ready
});
添加jQuery有助于防止与其他JS框架发生冲突。
为什么会发生冲突? 冲突通常发生,因为许多JavaScript库/框架使用相同的快捷方式 名称是美元符号$。然后,如果它们具有相同的命名功能,则浏览器获得 困惑!
我们如何预防冲突? 好吧,为了防止冲突,我建议别名jQuery命名空间(即使用上面的示例3)。 然后当你调用$ .noConflict()以避免名称空间困难时(因为$快捷方式不再可用) 我们每次都需要它时强迫jQuery jQuery。
jQuery.noConflict(); // Reverts '$' variable back to other JS libraries
jQuery(document).ready( function(){
//do jQuery stuff when DOM is ready with no conflicts
});
//or the self executing function way
jQuery.noConflict();
(function($) {
// code using $ as alias to jQuery
})(jQuery);
文档就绪示例4
(function($) {
// code using $ as alias to jQuery
$(function() {
// more code using $ as alias to jQuery
});
})(jQuery);
// other code using $ as an alias to the other library
这样你就可以在函数中嵌入一个函数,它都使用$作为jQuery别名。
文件就绪示例5
$(window).load(function(){
//initialize after images are loaded
});
有时候你想要操纵图片和$(文件).ready()你将无法做到这一点 如果访问者没有已加载的图像。在这种情况下,您需要初始化 图像完成加载时的jQuery对齐功能。
您也可以使用纯JavaScript并在html中附加一个函数调用body标签,只有在不使用JS框架时才使用它。
答案 1 :(得分:0)
只有let gesture = UITapGestureRecognizer(target: self, action:Selector("onHeartTap"))
具有let gesture = UITapGestureRecognizer(target: self, action:Selector("onHeartTap:"))
函数(在DOM完全加载时执行的函数)。也就是说,您可以执行以下操作:
document
答案 2 :(得分:0)
// same as $(document).ready() |$(document).on('ready) etc.
$(function(){
function loader(){
var $element = $('Your Pointer to element');
//If the number of elements matching the pointer is greater than 0
if($element.length > 0) {
//its loaded
}
else {
//since it isn't loaded , repeat in 250ms to see if it is
setTimeout(function(){ loader(); }, 250);
}
}
这是一种方法。它不是一个完美的解决方案,因为你从来没有真正想要在脚本中使用setTimeouts,但是因为我不知道你的其余代码在做什么,所以这是我能做的最好的建议。