美文网首页
jQuery基本操作

jQuery基本操作

作者: 奔跑的老少年 | 来源:发表于2018-08-28 16:47 被阅读0次

1、文本操作:

     $(..).text() ------获取文本内容
     $(..).text(‘<a>hhhhh</a>’) ------设置文本内容
     $(..).html() ------获取文本内容
     $(..).html(‘<a>hhhhh</a>’) ------设置文本内容
     $(..).val() ------获取value值
     $(..).val(..) ------设置value值 

2、样式操作:
addClass
removeClass
toggleClass
例如:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .hide{
            display: none;
        }
    </style>
</head>
<body>
<input type="button" id="i1" value="开关">
<div class="c1">jdfjgfdgdf</div>
<script src="jquery-3.3.1.js"></script>
<script>
    $('#i1').click(function () {
        // if($('.c1').hasClass('hide')){
        //     $('.c1').removeClass('hide');
        // }else{
        //     $('.c1').addClass('hide');
        // }
        //toggleClass就相当于以上的if语句
        $('.c1').toggleClass('hide')
    })
</script>
</body>
</html>

3、属性操作:

    专门用于做自定义属性:
    $(..).attr(‘n’)  ----获取属性n的值
    $(..).attr('n','v')----设置属性n的值
    $(..).removeAttr('n','v')

    专门用于checkbox、radio
    $(..).prop('checked')--获取选中状态
    $(..).prop('checked','ture')--设置选中状态

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<input type="button" value="全选" onclick="checkAll();">
<input type="button" value="反选" onclick="reverseAll();">
<input type="button" value="取消" onclick="cancelAll();">

<table border="1">
    <thead></thead>
    <tr>
        <th>选项</th>
        <th>ip</th>
        <th>port</th>
    </tr>
    <tbody id="tb">
    <tr>
        <td><input type="checkbox"></td>
        <td>1.1.1</td>
        <td>80</td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>1.1.1</td>
        <td>80</td>
    </tr>
    <tr>
        <td><input type="checkbox"></td>
        <td>1.1.1</td>
        <td>80</td>
    </tr>
   
    </tbody>
</table>
<script src="jquery-3.3.1.js"></script>
<script>
    function checkAll() {
        //prop('checked',true)设置选中状态
        $(':checkbox').prop('checked',true);
    }
    function cancelAll() {
        $(':checkbox').prop('checked',false);
    }
    function reverseAll() {
        $(':checkbox').each(function(){
            // 方法一:
            // if(this.checked){
            //     this.checked = false;
            // }else {
            //     this.checked = true;
            // }


            //方法二:
            //prop('checked')获取选中状态
            // if($(this).prop('checked')){
            //     $(this).prop('checked',false)
            // }else {
            //      $(this).prop('checked',true)
            // }

            //方法三:
            var v= $(this).prop('checked')?false:true
            $(this).prop('checked',v)

        })
    }
</script>
</body>
</html>

相关文章

  • jQuery 基础

    jQuery jQuery操作DOM jQuery的选择器 基本选择器 层级选择器 过滤选择器 jQuery操作样...

  • jQuery

    jQuery jQuery操作DOM jQuery的选择器 基本选择器 层级选择器 过滤选择器 jQuery操作样...

  • JQuery基础知识

    jQuery操作DOM jQuery的选择器 基本选择器 层级选择器 过滤选择器 jQuery操作样式 css操作...

  • 2018-04-08

    JQuery 赋值基本操作整理,(转)

  • jQuery基本操作

    jquery选择器 选择集转移 click事件 jquery索引值 jquery选项卡 jQuery属性操作

  • JQuery基本操作

    (1)jQuery加载 jQuery加载 // alert();//弹...

  • jQuery基本操作

    1、文本操作: 2、样式操作:addClassremoveClasstoggleClass例如: 3、属性操作:

  • jQuery基本操作

    1、jQuery选择器 2、选择集转移 3、jQuery样式操作 4、click事件 5、jQuery索引值 6、...

  • jQuery基本操作

  • jQuery 基本操作

    一、jQuery选择器 二、选择集转移 pre()是同级的上一个元素 preAll()是同级的上面所有元素 nex...

网友评论

      本文标题:jQuery基本操作

      本文链接:https://www.haomeiwen.com/subject/jcrqwftx.html