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.

  • 相关阅读:
    SQL Server 2008登录错误:无法连接到(local)解决方法
    HTML5 学习
    DNS服务器的配置与管理
    如何把TOMCAT 添加到服务中自动启动
    如何获取WIN10 Program Files 文件夹下的文件操作权限
    Oracle PL/SQL入门语法点
    【Oracle XE系列之三】使用OMF方式手工创建Oracle XE数据库
    【Oracle XE系列之二】PLSQL Developer 远程连接Oracle XE数据库
    【Oracle XE系列之一】Windows10_X64环境 安装Oracle XE11gR2 X64数据库
    Spark 调优
  • 原文地址:https://www.cnblogs.com/daxiong225/p/8006658.html
Copyright © 2011-2022 走看看