GVKun编程网logo

javascript – 在文档内部或外部使用jquery准备好

16

在这篇文章中,我们将带领您了解javascript–在文档内部或外部使用jquery准备好的全貌,同时,我们还将为您介绍有关javascript–JQuery$(‘iframe’).准备好为什么不起作

在这篇文章中,我们将带领您了解javascript – 在文档内部或外部使用jquery准备好的全貌,同时,我们还将为您介绍有关javascript – JQuery $(‘iframe’).准备好为什么不起作用?、javascript – jQuery NewBie问题:与$(文件)有什么关系.(准备好了)?、javascript – 使用Jquery Validate插件在文本字段上动态设置最小值、javascript – 使用jQuery删除类的知识,以帮助您更好地理解这个主题。

本文目录一览:

javascript – 在文档内部或外部使用jquery准备好

javascript – 在文档内部或外部使用jquery准备好

下面两个场景给了我相同的行为.但技术上有什么不同? (我将下面的代码放在正文中脚本标记的最后一部分.)

$(document).ready(function() {
  $('.collapse').collapse({toggle: false});

  $(document).on('click', '#expandAllLessons', function() {
    $('div.accordion-body').collapse('show');
  });

  $(document).on('click', '#collapseAllLessons', function() {
    $('div.accordion-body.collapse').collapse('hide');
  });
});

要么

$(document).ready(function() {
  $('.collapse').collapse({toggle: false});
});

$(document).on('click', '#expandAllLessons', function() {
  $('div.accordion-body').collapse('show');
});
$(document).on('click', '#collapseAllLessons', function() {
  $('div.accordion-body.collapse').collapse('hide');
});

谢谢.

解决方法:

或多或少,它正在做同样的事情.

通过将.on()与子选择器一起使用,您将使用事件委派将任何将来的事件绑定到与该选择器匹配的任何元素. document是DOM树的最顶层(并且在脚本执行时可用),因此您的事件委托可以正常工作.

.ready()等待DOM组装,因此您可以更可靠地使用.click(),..hover()等方法直接绑定事件.

所以你的第一个例子就是等待DOM组装,然后委托事件.第二个示例是在脚本执行时立即委派事件.

From jQuery’s documentation regarding .on():

Direct and delegated events

The majority of browser events bubble, or
propagate, from the deepest, innermost element (the event target) in
the document where they occur all the way up to the body and the
document element. In Internet Explorer 8 and lower, a few events such
as change and submit do not natively bubble but jQuery patches these
to bubble and create consistent cross-browser behavior.

If selector is omitted or is null, the event handler is referred to as
direct or directly-bound. The handler is called every time an event
occurs on the selected elements, whether it occurs directly on the
element or bubbles from a descendant (inner) element.

When a selector is provided, the event handler is referred to as
delegated. The handler is not called when the event occurs directly on
the bound element, but only for descendants (inner elements) that
match the selector. jQuery bubbles the event from the event target up
to the element where the handler is attached (i.e., innermost to
outermost element) and runs the handler for any elements along that
path matching the selector.

javascript – JQuery $(‘iframe’).准备好为什么不起作用?

javascript – JQuery $(‘iframe’).准备好为什么不起作用?

我的父页面中有一个非常长的iframe.当您重新加载或单击i框架页面内的链接时,它会被加载到其中,但是必须向上滚动纯粹的窗口.

我试过variuos代码示例:jquery which event is better thatn this和
How to scroll parent page with iFrame reload
父页面正文代码:

<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
    $('iframe').load(function(){
         $(window).scrollTop(0);
    });
});</script>
<iframe frameborder="0" height="1000" id="iframe" src="http://mysite.com" width="800"></iframe>

现在这是我在父页面中的最终代码:(这不起作用)

<script type="text/javascript">
$('iframe').ready(function(){
     $(window).scrollTop(0);
});
</script>
<iframe frameborder="0" height="1000" id="iframe" align="top" src="http://foicam.altervista.org/listadinamica.PHP" width="800" ></iframe>

但问题是每个代码在帧完全加载(图像包括)之后向上滚动父页面.我想要的是父页面在iframe完成加载之前向上滚动.

如果你点击最后一张图片或向下滚动页面,然后只重新加载iframe,你会明白我的意思!

很多,很抱歉,但我发现这个语言存在于今天下午!

解决方法

将您的代码更改为:
<iframe frameborder="0" height="1000" id="iframe" align="top" src="http://foicam.altervista.org/listadinamica.PHP" width="800" ></iframe>

<script type="text/javascript">
$('iframe').ready(function(){
     $(window).scrollTop(0);
});
</script>

在iframe成为DOM的一部分之前,您的脚本正在调用.如果你的iframe是你的DOM的一部分之后你的脚本,它将识别它,$(‘iframe’)将实际找到该对象.

javascript – jQuery NewBie问题:与$(文件)有什么关系.(准备好了)?

javascript – jQuery NewBie问题:与$(文件)有什么关系.(准备好了)?

我是jQuery的新手,只是在最近几天尝试学习它.在我的办公室里,很少有 JavaScript Developer的经验,他们主要使用jQuery来满足他们的所有需求,每当我和他们交谈以便更好地理解jQuery是如何工作的时候,他们首先说的就是$(document).准备好了你这样做,并在$(文件).(准备好)你这样做.

所以我的主要问题是什么是$(文件).(准备好了)它是如何运作的?

任何投入都将受到高度赞赏.

更新:在接受的答案评论中提到了当DOM准备就绪时,这究竟意味着什么?

解决方法

一旦DOM完全加载并准备好进行操作,$(document).ready()就会触发.这可以防止您的代码在它将对其存在的对象存在之前触发. $(document).ready()是它最详细的版本,可以用这些语句中的任何一个替换掉……
$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)
$(document).bind("ready",handler)

有关文档,请参见here.

javascript – 使用Jquery Validate插件在文本字段上动态设置最小值

javascript – 使用Jquery Validate插件在文本字段上动态设置最小值

我试图在一个字段上有一个动态更新的最小值,具体取决于来自其他字段的输入.简而言之,我的代码是:

$("#new_project").live("click", function() {

    switch($('input:radio[name=quality-level]:checked').val()){
        case 'average' : ppw = .006;
        case 'below-average' : ppw =.004;
        case 'good' : ppw = .008;
        case 'very-good' : ppw = .016;
    }

    if ($('#minimum-word-length').val() && $('input:radio[name=quality-level]:checked').val())
    {
        min_price = $('#minimum-word-length').val() * ppw;

    }

    $("#new_project, .edit_project").validate({
    rules: {
    ...
    "price-per-article": {required: true, number: true,  min:min_price},
    ...
    },
    errorPlacement: function(error, element) { }


    });

});

最低价格设置正确,并正确更新.也就是说,无论出于何种原因,最小值规则都不会更新我认为是因为验证代码仅加载在文档加载上.所以我猜有没有办法重新加载规则,以便在填充两个必要字段时最小值发生变化?

谢谢!

解决方法:

问题是您在初始加载时使用每篇文章的价格设置验证对象.我不熟悉验证插件,但一般情况下,如果您的属性实时更改了对象,您将需要为该属性使用回调函数,而不是在加载时设置它的数据.

因此,对于每篇文章的价格,它可能看起来像:

"price-per-article": {
  required: true,
  number: true,
  min: function () { return $('#minimum-word-length').val() * ppw; }
}

javascript – 使用jQuery删除类

javascript – 使用jQuery删除类

早上,

是否有一个我可以在jQuery中使用的通配符,它​​会从元素中删除一个类?

我正在将新类应用到我的信息消息框中,如果用户单击它一次,然后再次单击它就会添加新类,但保留现有类.

我需要删除上一个类,然后添加新类的样式.有任何想法吗?

 
display: block; ">All submitted Feeds are complete.

正如你可以看到原来的课程是绿色的,它已经添加了红色.

最佳答案
叶:

$(this).removeClass('er-green');
$(this).addClass('er-red');

或者先将它们全部删除:

If a class name is included as a parameter,then only that class will be removed from the set of matched elements. If no class names are specified in the parameter,all classes will be removed.

$(this).removeClass();
$(this).addClass('er-red');

关于javascript – 在文档内部或外部使用jquery准备好的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于javascript – JQuery $(‘iframe’).准备好为什么不起作用?、javascript – jQuery NewBie问题:与$(文件)有什么关系.(准备好了)?、javascript – 使用Jquery Validate插件在文本字段上动态设置最小值、javascript – 使用jQuery删除类的相关知识,请在本站寻找。

本文标签: