首页 >> js开发 >> jsjQuery HTML设置内容和属性操作实例分析js大全
jsjQuery HTML设置内容和属性操作实例分析js大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
本文实例讲述了jQuery HTML设置内容和属性操作。分享给大家供大家参考,具体如下:jQuery - 设置内容和属性设置内容 - text()、html() 以及 val()将使用前一章中的三个相同的方法来设置内容:
text() - 设置或返回所选元素的文本内容
html() - 设置或返回所选元素的内容(包括 HTML 标记)
val() - 设置或返回表单字段的值
text() - 设置或返回所选元素的文本内容html() - 设置或返回所选元素的内容(包括 HTML 标记)val() - 设置或返回表单字段的值
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("Hello world!");
});
$("#btn3").click(function(){
$("#test3").val("");
});
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("Hello world!");
});
$("#btn3").click(function(){
$("#test3").val("");
});
text()、html() 以及 val() 的回调函数上面的三个 jQuery 方法:text()、html() 以及 val(),同样拥有回调函数。回调函数有两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。下面的例子演示带有回调函数的 text() 和 html():
$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "旧文本: " + origText + " 新文本: Hello world! (index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "旧 html: " + origText + " 新 html: Hello world! (index: " + i + ")";
});
});
$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "旧文本: " + origText + " 新文本: Hello world! (index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "旧 html: " + origText + " 新 html: Hello world! (index: " + i + ")";
});
});
设置属性 - attr()jQuery attr() 方法也用于设置/改变属性值。下面的例子演示如何改变(设置)链接中 href 属性的值:
$("button").click(function(){
$("#").attr("href","///jquery");
});
$("button").click(function(){
$("#").attr("href","///jquery");
});
attr() 方法也允许同时设置多个属性。下面的例子演示如何同时设置 href 和 title 属性:
$("button").click(function(){
$("#").attr({
"href" : "///jquery",
"title" : "jQuery 教程"
});
});
$("button").click(function(){
$("#").attr({
"href" : "///jquery",
"title" : "jQuery 教程"
});
});
attr() 的回调函数jQuery 方法 attr(),也提供回调函数。回调函数有两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。下面的例子演示带有回调函数的 attr() 方法:
$("button").click(function(){
$("#").attr("href", function(i,origValue){
return origValue + "/jquery";
});
});
$("button").click(function(){
$("#").attr("href", function(i,origValue){
return origValue + "/jquery";
});
});
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools./code/HtmlJsRun测试上述代码运行效果。在线HTML/CSS/JavaScript代码运行工具在线HTML/CSS/JavaScript代码运行工具http://tools./code/HtmlJsRun关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery页面元素操作技巧汇总》、《jQuery常见事件用法与技巧总结》、《jQuery常用插件及用法总结》、《jQuery扩展技巧总结》及《jquery选择器用法总结》jQuery页面元素操作技巧汇总jQuery常见事件用法与技巧总结jQuery常用插件及用法总结jQuery扩展技巧总结jquery选择器用法总结希望本文所述对大家jQuery程序设计有所帮助。
text() - 设置或返回所选元素的文本内容
html() - 设置或返回所选元素的内容(包括 HTML 标记)
val() - 设置或返回表单字段的值
text() - 设置或返回所选元素的文本内容html() - 设置或返回所选元素的内容(包括 HTML 标记)val() - 设置或返回表单字段的值
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("Hello world!");
});
$("#btn3").click(function(){
$("#test3").val("");
});
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("Hello world!");
});
$("#btn3").click(function(){
$("#test3").val("");
});
text()、html() 以及 val() 的回调函数上面的三个 jQuery 方法:text()、html() 以及 val(),同样拥有回调函数。回调函数有两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。下面的例子演示带有回调函数的 text() 和 html():
$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "旧文本: " + origText + " 新文本: Hello world! (index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "旧 html: " + origText + " 新 html: Hello world! (index: " + i + ")";
});
});
$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "旧文本: " + origText + " 新文本: Hello world! (index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "旧 html: " + origText + " 新 html: Hello world! (index: " + i + ")";
});
});
设置属性 - attr()jQuery attr() 方法也用于设置/改变属性值。下面的例子演示如何改变(设置)链接中 href 属性的值:
$("button").click(function(){
$("#").attr("href","///jquery");
});
$("button").click(function(){
$("#").attr("href","///jquery");
});
attr() 方法也允许同时设置多个属性。下面的例子演示如何同时设置 href 和 title 属性:
$("button").click(function(){
$("#").attr({
"href" : "///jquery",
"title" : "jQuery 教程"
});
});
$("button").click(function(){
$("#").attr({
"href" : "///jquery",
"title" : "jQuery 教程"
});
});
attr() 的回调函数jQuery 方法 attr(),也提供回调函数。回调函数有两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。下面的例子演示带有回调函数的 attr() 方法:
$("button").click(function(){
$("#").attr("href", function(i,origValue){
return origValue + "/jquery";
});
});
$("button").click(function(){
$("#").attr("href", function(i,origValue){
return origValue + "/jquery";
});
});
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools./code/HtmlJsRun测试上述代码运行效果。在线HTML/CSS/JavaScript代码运行工具在线HTML/CSS/JavaScript代码运行工具http://tools./code/HtmlJsRun关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery页面元素操作技巧汇总》、《jQuery常见事件用法与技巧总结》、《jQuery常用插件及用法总结》、《jQuery扩展技巧总结》及《jquery选择器用法总结》jQuery页面元素操作技巧汇总jQuery常见事件用法与技巧总结jQuery常用插件及用法总结jQuery扩展技巧总结jquery选择器用法总结希望本文所述对大家jQuery程序设计有所帮助。
相关文章:
- js解决vue页面渲染但dom没渲染的操作js大全
- jsvue props default Array或是Object的正确写法说明js大全
- jsAngular利用HTTP POST下载流文件的步骤记录js大全
- js解决vue动态路由异步加载import组件,加载不到module的问题js大全
- jsElement Breadcrumb 面包屑的使用方法js大全
- jsVue中的this.$options.data()和this.$data用法说明js大全
- js在vue中使用防抖函数组件操作js大全
- jsvue 页面回退mounted函数不执行的解决方案js大全
- js谈一谈vue请求数据放在created好还是mounted里好js大全
- jsvue项目使用$router.go(-1)返回时刷新原来的界面操作js大全