<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
//通过text()设置P标签的值
$("#but1").click(function(){
$("#p1").text("This is a paragraph");
});
//通过val()设置P标签的值
$("#but2").click(function(){
$("#p1").val("This is a paragraph");
});
//通过html()设置P标签的值
$("#but3").click(function(){
$("#p1").html("<b>This is a paragraph</b>");
});
//通过text()设置输入框的值
$("#but4").click(function(){
$("input").text("this is a text");
});
//通过val()设置输入框的值
$("#but5").click(function(){
$("input").val("this is a text");
});
//通过html()设置输入框的值
$("#but6").click(function(){
$("input").html("<b>This is a text</b>");
});
$("#but7").click(function(){
$("a").attr("href","ajax.googleapis.com");
$("a").text("ajax.googleapis.com");
});
});
</script>
</head>
<body>
<!-- <form action=""> -->
<p id="p1">这是段落中的<b>粗体</b>文本。</p>
<button id="but1">通过text()设置值</button>
<button id="but2">通过val()设置值</button>
<button id="but3">通过html()设置值</button><br><br>
<input type="text" value="这是输入框中的<b>粗体</b>文本。"><br><br>
<button id="but4">通过text()设置值</button>
<button id="but5">通过val()设置值</button>
<button id="but6">通过html()设置值</button><br>
<p><a href="http://www.baidu.com" >baidu.com</a></p>
<button id="but7">设置 href 值</button>
<!-- </form> -->
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
//通过text()设置P标签的值
$("#but1").click(function(){
$("#p1").text("This is a paragraph");
});
//通过val()设置P标签的值
$("#but2").click(function(){
$("#p1").val("This is a paragraph");
});
//通过html()设置P标签的值
$("#but3").click(function(){
$("#p1").html("<b>This is a paragraph</b>");
});
//通过text()设置输入框的值
$("#but4").click(function(){
$("input").text("this is a text");
});
//通过val()设置输入框的值
$("#but5").click(function(){
$("input").val("this is a text");
});
//通过html()设置输入框的值
$("#but6").click(function(){
$("input").html("<b>This is a text</b>");
});
$("#but7").click(function(){
$("a").attr("href","ajax.googleapis.com");
$("a").text("ajax.googleapis.com");
});
});
</script>
</head>
<body>
<!-- <form action=""> -->
<p id="p1">这是段落中的<b>粗体</b>文本。</p>
<button id="but1">通过text()设置值</button>
<button id="but2">通过val()设置值</button>
<button id="but3">通过html()设置值</button><br><br>
<input type="text" value="这是输入框中的<b>粗体</b>文本。"><br><br>
<button id="but4">通过text()设置值</button>
<button id="but5">通过val()设置值</button>
<button id="but6">通过html()设置值</button><br>
<p><a href="http://www.baidu.com" >baidu.com</a></p>
<button id="but7">设置 href 值</button>
<!-- </form> -->
</body>
</html>
本文介绍了如何使用jQuery库轻松地通过text(), val(), html()方法修改网页中的文本内容,包括段落、输入框和链接元素。
1256

被折叠的 条评论
为什么被折叠?



