jquery元素选择val()、html()、text()的用法

 时间:2026-03-02 09:55:46

1、在整个jquery开发框架之中,最为重要的工具就是页面元素的选择处理。

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%

        pageContext.setAttribute("APP_PATH",request.getContextPath());

%>

<script type="text/javascript" src="${APP_PATH}/static/js/jquery-3.2.1.min.js"></script>

<link rel="stylesheet" href="${APP_PATH}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css" />

<script type="text/javascript" src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

<script type="text/javascript">

        function fun() {

                var msgValue = $(msg).val();

                console.log("**************msg=" + msgValue);

        }

</script>

<html>

        <body>

                <input type="text" name="msg" id="msg">

                <input type="button" value="选择" onclick="fun()">

        </body>

</html>

<script type="text/javascript" src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

<script type="text/javascript">

        function fun() {

              

                    var msgValue = $(msg).val();

                     $(showMsg).text(msgValue);

        }

</script>

<html>

        <body>

                <input type="text" name="msg" id="msg">

                <input type="button" value="设置内容" onclick="fun()">

                

                <div id="showMsg"></div>

        </body>

</html>

jquery元素选择val()、html()、text()的用法

5、但是text()设置的都是纯文本,如果此时你输入的内容包含有html元素。

<script type="text/javascript" src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

<script type="text/javascript">

        function fun() {

               

          var msgValue = $(msg).val();

          $(showMsg).html(msgValue);

        }

</script>

<html>

        <body>

                <input type="text" name="msg" id="msg">

                <input type="button" value="设置内容" onclick="fun()">

                

                <div id="showMsg"></div>

        </body>

</html>

jquery元素选择val()、html()、text()的用法

<script type="text/javascript" src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

<script type="text/javascript">

        function fun() {

                console.log($(showMsg).text());

                console.log($(showMsg).html());

        }

</script>

<html>

        <body>

                <input type="text" name="msg" id="msg">

                <input type="button" value="设置内容" onclick="fun()">

                

                <div id="showMsg"><h1>www.baidu.com</h1></div>

        </body>

</html>

jquery元素选择val()、html()、text()的用法

jquery元素选择val()、html()、text()的用法

8、面试题:请问text()和html()有什么区别?

  • jQuery中的text(),html(),val()有什么区别
  • 如何区分jquery里的html()和text()方法
  • jQuery:text 选择器选取类型为text的input元素
  • jQuery获取text、html、属性值、value的方法
  • jquery通过class获得的元素是个什么类型的?
  • 热门搜索
    如何提高网速 腐女是什么意思 网上怎么做生意 涵字取名的寓意是什么 如何抠图换背景 如何清肠排宿便 最生活 pdg文件怎么打开 空白网名怎么弄 泰坦尼克号怎么拍的