GVKun编程网logo

javascript – 来自追加的jquery .on()(javascript来源)

16

在本文中,我们将为您详细介绍javascript–来自追加的jquery.on()的相关知识,并且为您解答关于javascript来源的疑问,此外,我们还会提供一些关于Bootstrap的JavaSc

在本文中,我们将为您详细介绍javascript – 来自追加的jquery .on()的相关知识,并且为您解答关于javascript来源的疑问,此外,我们还会提供一些关于Bootstrap的JavaScript需要jQuery,而jQuery已加载、javascript – .prop的JQuery typescript定义?、javascript – JQuery 1.7中的JSONP错误、javascript – jQuery Mobile – 无法使用常见的jquery方法隐藏按钮的有用信息。

本文目录一览:

javascript – 来自追加的jquery .on()(javascript来源)

javascript – 来自追加的jquery .on()(javascript来源)

我真的很困惑如何将click事件添加到jquery创建的元素中.

现在,我生成这样的元素:

$.ajax(
    params....
)
.done(function(item){
    $.each(item, function(i, some){
        a = '<p>some text</p>';
        $(node).before(a);
    }
});

我的问题是当我尝试将click事件添加到元素“p”时.如果我做:

$('p').on('click', 'p', function(){
    alert('yai!');
});

什么也没显示.如果我这样做,请坚持:

$.ajax(
    params....
)
.done(function(item){
    $.each(item, function(i, some){
        a = '<p>some text</p>';
        $(a).click(function(){
            alert('yai!');
        });
        $(node).before(a);
    }
});

它显示太多警报(相同数量的p元素)

我做错了什么?

解决方法:

你的选择:

>(推荐)您可以在父元素上绑定该click事件.这是最好的方法,因为无论父级内部的p数是多少,点击总是附加到父级,这使得DRY更加高效.

$("YourParentElement").on('click', 'p', function(){
   alert('yai!');
});

>在$.each循环之后,您可以使用$(“p”)作为选择器放置事件处理程序.那时你的p元素将在DOM中,因此你可以将click处理程序绑定到元素. (缺点:你将点击处理程序附加到每个p,如果你有大量类似的ps,这有时是一个开销)

$.ajax(
  params....
).done(function(item){
   $.each(item, function(i, some){
       a = '<p>some text</p>';   
       // Click() was here causing it ////////
       //  to loop multiple times           //
       $(node).before(a);                   //
   }); ///////////////////////////////////////
       //
     / / / moved here
    $("p").click(function(){
        alert('yai!');
    });
 });

>即使在加载jquery之前,您也可以将此元素(p)绑定到始终存在的文档对象.它现在不在API中.走这些路线,使用直播绝对不是更好.有关为什么生活是邪恶的信息,请转到here和here.

Bootstrap的JavaScript需要jQuery,而jQuery已加载

Bootstrap的JavaScript需要jQuery,而jQuery已加载

我在加载Bootstrap库时遇到错误,因为它总是会出现此错误:

Uncaught Error: Bootstrap’s JavaScript requires jQuery

尽管如此,在确保已加载jQuery但仍出现错误之后,我仍附加了Bootstrap库.

我正在使用以下代码通过创建元素并将jQuery附加到页面并将其附加到文档:

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '3.1.1') {
    console.log("jQuery LOADED");
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type", "text/javascript");
    script_tag.setAttribute("src",
        "http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js");


    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);


    if (script_tag.readyState) {
        script_tag.onreadystatechange = function () { // For old versions of IE
            if (this.readyState == 'complete' || this.readyState == 'loaded') {
                console.log(window.jQuery.fn.jquery);
                scriptLoadHandler();
            }
        };
    } else {
        console.log("ONLOAD STATE");
        script_tag.onload = scriptLoadHandler;
    }
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}

function scriptLoadHandler() {
    // Restore $and window.jQuery to their prevIoUs values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery.noConflict(true);
    // Call our main function
    main();
}

准备好文档后,在这里创建Bootstrap:

function main() {

    jQuery(document).ready(function ($) {
    var bootstrap_script = document.createElement('script');
    bootstrap_script.setAttribute("type", "text/javascript");
    bootstrap_script.setAttribute("src",
    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js");

    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(bootstrap_script);
    })
}

只是为了阐明一件重要的事情
我在一个单独的js文件中编写js代码,因为我想以后用作jQuery插件.因此,我尝试附加库,因为我不保证目标页面会包含或不包含这些库

解决方法:

尝试使用https加载jquery

script_tag.setAttribute("src",
        "https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js");

希望它能工作.

javascript – .prop的JQuery typescript定义?

javascript – .prop的JQuery typescript定义?

我有以下代码:

// Populate the title of the selects on change and trigger a change
$('#modal .update-title')
    .change(function () {
        var $select = $(this),
        match = null,
        matchRow = null,
        title = $('option:selected', this).prop('title');

在typescript中,它将title的值设置为bool.然后,当我尝试按如下方式使用它时,我收到一个错误:

$("#modal_Title_" + matchRow).val(title);

这是JQuery定义的另一个问题还是我做错的事情?检查定义文件时,我看到以下内容:

prop(propertyName: string): bool;

这似乎与JQuery doc不匹配.

解决方法:

jQuery文档不正确,您的类型定义文件也是如此.

大多数属性是字符串,但有些是数字(例如.selectedindex),有些是布尔值(.checked等),而$.fn.prop()不会将它们全部转换为字符串.

正确的定义应该是:

prop(propertyName: string): any;

TypeScript网站上已经列出了一个work item来解决这个问题.

javascript – JQuery 1.7中的JSONP错误

javascript – JQuery 1.7中的JSONP错误

注意:我已经检查了其他答案,包括this question re: a pre-jQuery 1.5 JSONP error issue that has now been resolved.我正在使用JQuery 1.7,我相信不会遇到这个问题.

截至JQuery 1.5 proper error functions are possible with JSONP(参见’jqXHR对象’).但是,错误功能似乎没有触发.

$.getJSON(url, function(response) { 
    console.log(response)   
}).error( function() { console.log('Error occurred.') } )   

查看Chrome Dev Tools,JSONP请求会生成以下错误:

GET https://twitter.com/status/user_timeline/somepretendusername.json?count=2&callback=jQuery171005595548846758902_1334772179012&_=1334772179132 400 (Bad Request)

但是.error()回调似乎没有运行.使用JSONP进行正确的错误处理需要什么?

解决方法:

只有在请求出错时才会触发错误功能.请求正在进行,但如果用户名不存在,Twitter将返回400.我尝试使用假的用户名并得到你做的相同的响应,但后来我使用了我的用户名,它就像一个冠军.

[编辑]我看到你只是在问如何使用JSONP处理错误.无论出于何种原因,似乎jQuery没有很好的错误处理.我确实找到了一个承诺为JSONP提供实际错误处理的插件.看看here.

[edit2]检查出来,它的工作原理!

$(document).ready(function() {
    var url = 'https://twitter.com/status/user_timeline/thisisnotarealtwitterhandle.json?count=2&callback=?';
    var jqxhr = $.jsonp({
        url: url,
        success: function(response) {
            console.log(response)
        }
    }).fail(function() { console.log('error'); });
});

javascript – jQuery Mobile – 无法使用常见的jquery方法隐藏按钮

javascript – jQuery Mobile – 无法使用常见的jquery方法隐藏按钮

我有这个非常简单的按钮,我想隐藏

<input type="button" id="logoutBtn" value="logout" 
       data-theme="d" data-inline="true" aria-disabled="false">

我正在使用这个简单的调用来尝试隐藏它,但它没有做任何事情

$('#logoutBtn').hide();

那么我尝试添加它,这也不起作用.

$('#first').live('pageinit', function(e){
      $('#logoutBtn').hide();
});

我究竟做错了什么?

解决方法:

可能重复jquery mobile cannot hide submit button

<div data-theme="e"aria-disabled="false">
    <span>
        <span>Submit</span>
    </span>
    <input type="submit" id="logoutBtn" value="button" data-theme="e" data-inline="true"aria-disabled="false">
</div>

$('#logoutBtn').closest('.ui-btn').hide();

今天关于javascript – 来自追加的jquery .on()javascript来源的分享就到这里,希望大家有所收获,若想了解更多关于Bootstrap的JavaScript需要jQuery,而jQuery已加载、javascript – .prop的JQuery typescript定义?、javascript – JQuery 1.7中的JSONP错误、javascript – jQuery Mobile – 无法使用常见的jquery方法隐藏按钮等相关知识,可以在本站进行查询。

本文标签: