[ jquery 属性选择器[key |= val] ] 此方法选取与给定表达式相匹配的元素

建议在观看学习本章之前有一定css选择器和正则表达式语法基础,这样不必死记硬背会好理解一点:

实例:

<!DOCTYPE html>
<html lang='zh-cn'>
<head>
<title>Insert you title</title>
<meta http-equiv='description' content='this is my page'>
<meta http-equiv='keywords' content='keyword1,keyword2,keyword3'>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type='text/javascript' src='./js/jquery-1.12.1.min.js'></script>
<style type='text/css'>
    *{margin:0;padding:0;}
    #wrap{800px;height:450px;margin:20px auto;}
    input{text-indent:8px;font:400 13px/1.2em 'Courier New';color:#999;99%;margin:5px 0;padding:8px 0;box-sizing:border-box;}
    .newClass{text-decoration: line-through; }
</style>
<script type='text/javascript'>
    $(function(){
        
        // 使用   [key = val] 并结合其他选择器实现对元素精确选取
        $('input[type="text"]').val('userName');
        
        // 使用   [key] 并结合其他选择器实现对元素精确选取
        $('input[type]').css('letter-spacing','1px');
        
         // 使用   [key != val] 并结合其他选择器实现对元素精确选取
        $("input[type!='password']").addClass('newClass');
        
        // 使用   [key ^= val] 并结合其他选择器实现对元素精确选取
        $('#target a[href ^= "http://"').css({'text-decoration':'none','color':'#333','font':'400 16px/1.2em "Courier New"','cursor':'pointer',});
        
        // 使用   [key $= val] 并结合其他选择器实现对元素精确选取
        $('#target a[href $= "hk"').css({'text-decoration':'line-through','color':'#666','font':'400 16px/1.2em "Courier New"','cursor':'pointer',});
        
        // 使用   [key *= val] 并结合其他选择器实现对元素精确选取
        $('#target a[href *= ".com"').css({'display':'inline-block','background':'GreenYellow','width':'49%','text-align':'center','height':'20px','font':'400 16px/20px "Courier New"',});
    });
</script>
</head>
<body>
    <div id='wrap'>
        <form id='form'>
            <input type='text' id='user' placeholder='user'/>
            <input type='password' id='pw'/>
        </form>
        <div id='target'>
            <a href='http://www.baidu.com' target='_blank'>baidu</a>
            <a href='https://www.goggle.com.hk' target='_blank'>goggle</a>
        </div>
    </div>
    
</body>
</html>
原文地址:https://www.cnblogs.com/mysearchblog/p/5606782.html