Home  >  Article  >  Web Front-end  >  Summary of some common methods of js_basic knowledge

Summary of some common methods of js_basic knowledge

WBOY
WBOYOriginal
2016-05-16 18:05:28692browse

Val(),append(),get(),split(),substr(),each(),html(),keyup(),trim(),show(),hide(),indexOf()
Look at it one by one:
Val(): to get and assign values ​​to a page element
Value: var result = $(“#txtSearch”).val();
Assignment :$("#txtSearch").val(result);

Each(): Operation on a collection, calling subsequent methods on each element in the collection, for example:
$. each(data.list, function (i, item) {
Alert(item["WikiTitle"]" ” item["WikiID"]);//i is the element of the collection in the following table, item represents the element itself
});

Append(): Append elements after the specified page element
For example, a list element

  • test!
  • , page elements like this can be appended Method to dynamically add multiple
  • items. For example:
    There is a div with id="div_keycontent" in the page:

    It can be done in Js Write like this:
    $.each(data.list, function (i, item) {
    $("#div_keycontent").append("
  • " item["WikiTitle"] " View" "
  • ");
    });

    Get(): on the page Obtaining data asynchronously, this method is a form of asynchronous binding, which is explained in my previous article, so I won’t go into it here.

    Spilt(): Operate on strings. For example:
    Var str = spit(“liu,ming,feng”,”,”);
    The str returned in this way is a string array : {"liu", "ming", "feng"}

    Substr(); For string operations, remove the substrings inside
    Usage:
    ///Judge the last one Whether the character is a comma
    if (str.substring(str.length - 1, str.length) == "," || str.substring(str.length - 1, str.length) == ",") {
    alert("The last character is a comma!");
    }

    Html(): Modify the content of an html tag, for example:
    $("#div_keycontent") .html("

    No data

    ");

    Keyup(); Method triggered by keyboard press and popup
    $("#txtSearchKey").keyup(function () {
    $("#div_keycontent").html("

    Data retrieval....

    ");
    });

    Trim( ): Remove the leading and trailing spaces of the string
    Usage: str.trim();

    Show(): Let the page elements be displayed. For example: $("#txtSearchKey").show();
    Hide(): Hide elements of the page. For example: $("#txtSearchKey").hide();

    Indexof(): Check whether there is a corresponding substring in the string
    Usage:
    if (str.indexOf(',,') != -1 || str.indexOf(',,') != -1) {///Determine whether there are consecutive commas
    alert("There are two Use commas together! ”);
    }

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn