本教程操作环境:windows10系统、jquery3.2.1版本、Dell G3电脑。
jquery中html方法实现了哪些功能
1、返回元素内容
当使用该方法返回一个值时,它会返回第一个匹配元素的内容。
语法
复制代码1
$(selector).html()
登录后复制
示例如下:
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".btn1").click(function(){
alert($("p").html());
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button class="btn1">改变 p 元素的内容</button>
</body>
登录后复制
输出结果:
点击按钮后:
2、设置元素内容
当使用该方法设置一个值时,它会覆盖所有匹配元素的内容。
语法
复制代码1
$(selector).html(content)
登录后复制
content 可选。规定被选元素的新内容。该参数可包含 HTML 标签。
示例如下:
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".btn1").click(function(){
$("p").html("Hello <b>world!</b>");
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button class="btn1">改变 p 元素的内容</button>
</body>
登录后复制
输出结果:
点击按钮后:
3、使用函数来设置元素内容
使用函数来设置所有匹配元素的内容。
语法
复制代码1
$(selector).html(function(index,oldcontent))
登录后复制
参数 描述
function(index,oldcontent)
规定一个返回被选元素的新内容的函数。
index - 可选。接收选择器的 index 位置。
oldcontent - 可选。接收选择器的当前内容。
示例如下:
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").html(function(n){
return "这个 p 元素的 index 是:" + n;
});
});
});
</script>
</head>
<body>
<p>这是一个段落。</p>
<p>这是另一个段落。</p>
<button>改变 p 元素的内容</button>
</body>
登录后复制
输出结果:
点击按钮后:
相关视频教程推荐:jQuery视频教程
以上就是jquery中html方法实现了哪些功能的详细内容,更多请关注靠谱客其它相关文章!
最后
以上就是小巧翅膀最近收集整理的关于jquery中html方法实现了哪些功能的全部内容,更多相关jquery中html方法实现了哪些功能内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复