jQuery操作HTML和CSS

来源:互联网 发布:ubuntu设置ip不起作用 编辑:程序博客网 时间:2024/04/28 00:05
<html>
<head>
<title></title>
<style type="text/css">
    .liclass{
        background-color:#333;
    }
</style>
<!-- jQuery库导入 -->
<script src="jquery.js"></script>
<script type="text/javascript">
    /* jQuery操作HTML和CSS */
    /* 操作HTML */
    /* 向DOM中添加HTML .html() */
    /* 向DOM中添加HTML前面或者后面 .append() .prepend() */
    /* 在某元素之前或者之后添加HTML .before() .after() */
    /* 删除HTML元素 .remove() */
    /* 复制HTML元素 .clone() */
    /* 设置文本内容 .text() */
    /* 操作CSS */
    /* 获取或者设置CSS .css() */
    /* 添加CSS类 .addClass() */
    /* 是否拥有CSS类 .hasClass() */
    /* 删除CSS类 .removeClass() */
    /* 切换CSS类 .toggleClass() 有则删除,没有则添加 */

    $(document).ready(
        function(){
            /* 通配符选择器* 选择所有页面元素 */
            //$("*").css("padding","10px");
        }
    )
    
    function insertNewLi(){
        $("#first ul").append("<li>new</li>");
    }
    function cloneNewLi(){
        $("#second ul li:eq(0)").after($("#second ul li:eq(0)").clone());
    }
    function removeLi(){
        $("#second ul li:eq(0)").remove();
    }
    function toggleLi(){
        $("#third ul li").toggleClass("liclass");
    }
</script>
</head>
<body>
    <div>
        <div id="first">
            <ul>
                <li>old</li>
                <li>old</li>
            </ul>
            <button onclick="insertNewLi();">插入新列</button>
        </div>
        <div id="second">
            <ul>
                <li>This is init Li!</li>
            </ul>
            <button onclick="cloneNewLi();">复制列</button>
            <button onclick="removeLi();">删除列</button>
        </div>
        <div id="third">
            <ul>
                <li>This is one Li!</li>
                <li>This is two Li!</li>
                <li>This is three Li!</li>
                <li>This is four Li!</li>
                <li>This is five Li!</li>
                <li>This is six Li!</li>
            </ul>
            <button onclick="toggleLi();">复制列</button>
        </div>
    </div>
</body>
</html>

原创粉丝点击