获取当前div中的文本(只获取当前div的, 子元素不要, 基于layui)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>test2</title>
        <link rel="stylesheet" type="text/css" href="res/layui/css/layui.css">
        <script type="text/javascript" src="res/layui/layui.js"></script>
    </head>
    <body>
        <div id="test-box">
            aaaaa
            <div>
                bbbbb
            </div>
        </div>

        <script type="text/javascript">
            layui.use(["layer"],function() {
                var layer = layui.layer;
                var $ = layui.jquery;

                var str = $('#test-box').contents().filter(function (index, content) {
                    console.log(index);
                    console.log(content);
                    return content.nodeType === 3;
                }).text();

                // alert($("#test-box").text());
                alert(str);
            });

        </script>
    </body>
</html>

 

猜你喜欢

转载自www.cnblogs.com/wgbs25673578/p/10364092.html
0条评论
添加一条新回复