JavaScript common operations methods

1. String reversal

var str = "abcdefghijklmn";
str = str.split('').reverse().join("");

2. Intercept part of the string (intercept from the character with subscript 1 to the last one)

var str = "abcdefghijklmn";
str = str.split('').slice(1, res.length).join("");

3. Determine whether the data type is undefined

var str;
if(typeof(str) == "undefined") console.log("str is undefined");

4. Rounding function

console.log(Math.round(1.53))//输出2
console.log(Math.round(1.13))//输出1
console.log(Math.round(1.92))//输出2
console.log(Math.round(1.50))//输出2
console.log(Math.round(1.49))//输出1

5. Get the id of the clicked object DOM

$(this).attr("id")

6. Get the index subscript of the clicked object DOM at this level

$(this).index()

7. Hide other sibling elements except itself

// 隐藏其他subContainer
$('.sub_container:eq(' + $(this).index() + ')').siblings().filter('.sub_container').css({'display': 'none'});

8. Click the first one by default

// 默认点击第一个
$('.sub_container:eq(' + $(this).index() + ') .sub_tab_item:eq(0)').click();

9.Use of map

10. Determine how many identical elements there are

<tbody>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
</tbody>
$("tbody tr").length

11. Determine whether there is a corresponding element

$('#no_use_data').length && $('#no_use_data').length > 0

12. Add elements before/after the specified DOM element

let yx_sub_class_tab_html = '<div>Hello World</div>';
// 前
$(".tab-content").parent().before(yx_sub_class_tab_html);
// 后
$(".tab-content").parent().after(yx_sub_class_tab_html);

Guess you like

Origin blog.csdn.net/weixin_43823060/article/details/131416366