zoukankan      html  css  js  c++  java
  • Js/jquery常用

    id属性不能有空格

    1. js判断checkebox是否被选中

    var ischecked = document.getElementById("xxx").checked  //返回false or true

    设置选上/不选:document.getElementById("xxx").checked=true/false

    Jquery

     判断选上:$('#isAgeSelected').attr('checked') // true//false

    选上:

    $('.myCheckbox').prop('checked', true) or
    $('.myCheckbox').attr('checked', true) or
    $(".myCheckBox").checked(true);

    不选改为false

    2. jquery 判断selecte的值

    <select name="selector" id="selector">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    </select>

    $('select[name=selector]').val()

    选中Option 1

    $('select[name=selector]').val("1");

    3. 往Array中存入对象,

    var regions = new Array();

    var ri = new Object();
    ri.id = "";
    ri.name = "";
    regions.push(ri);
    <c:forEach items="${regionList.values}" var="region">
    ri = new Object();
    ri.id = "${region.id}";
    ri.name = "${region.name}";
    regions.push(ri);
    </c:forEach>
    window.regions = regions;

    取数据则 Object region = regions[index];

    var regionId = region.id;

    Note: js中嵌入了<c:foreach>不建议这么用。

    4. bind 与 unbind

    $("#name").bind("click", (function () {
    $("#attrib_UNIQUE_ACTV_PURCHASE").attr('checked', true);
    }));
    $("#description").bind("click", (function () {
    $("#attrib_UNIQUE_ACTV_PURCHASE").attr('checked', false);
    }));
    $("#message").bind("click", (function () {
    $("#name").unbind("click");
    $("#description").unbind("click");
    }));

    增加点击后选中/不选中或解绑后点击效果

    click为eventType,还有其他的

    Mouse EventsKeyboard EventsForm EventsDocument/Window Events
    click keypress submit load
    dblclick keydown change resize
    mouseenter keyup focus scroll
    mouseleave   blur unload

    5.判断元素是否存在:

    How do I test whether an element exists?

    Use the .length property of the jQuery collection returned by your selector:

    1
    2
    3
    4
    5
    if ( $( "#myDiv" ).length ) {
     
    $( "#myDiv" ).show();
     
    }

    Note that it isn't always necessary to test whether an element exists. The following code will show the element if it exists, and do nothing (with no errors) if it does not:

    $( "#myDiv" ).show();

    or:

    It seems some people are landing here, and simply want to know if an element exists (a little bit different to the original question).

    That's as simple as using any of the browser's selecting method, and checking it for a truthy value (generally).

    For example, if my element had an id of "find-me", I could simply use...

    var elementExists = document.getElementById("find-me");
    if (
    elementExists != null){dosomething}

    This is spec'd to either return a reference to the element or null.

  • 相关阅读:
    通过Javascript调用微软认知服务情感检测接口的两种实现方式
    Microsoft Flow 概览
    使用PowerApps快速构建基于主题的轻业务应用 —— 进阶篇
    从三个语言(C++,Java,C#)的几个性能测试案例来看性能优化
    自己动手,打造轻量级VSCode/C#环境代替LinqPad
    2015年总结
    将知识变成你的技能点
    瞎子摸象与刻舟求剑
    俺的追求,下一个五年的指导纲领
    工作中任务管理的四个原则和四个技能
  • 原文地址:https://www.cnblogs.com/daxiong225/p/8006658.html
Copyright © 2011-2022 走看看