分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

jQuery初学3(HTML)

发布时间:2023-09-06 02:21责任编辑:赖小花关键词:HTMLjQuery
  1. text(),html(),val().

    text()是设置或返回所选元素的文本内容。
    html()是 设置或返回所选元素内容,包含标签。
    val()是返回表单字段中的值。
    <!DOCTYPE html><html><head><meta charset="utf-8"><script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script><script>$(document).ready(function(){ ?$("#btn1").click(function(){ ???alert("Text: " + $("#test").text()); ?}); ?$("#btn2").click(function(){ ???alert("HTML: " + $("#test").html()); ?});});</script></head><body><p id="test">这是段落中的 <b>粗体</b> 文本。</p><button id="btn1">显示文本</button><button id="btn2">显示 HTML</button></body></html>
    $("button").click(function(){ ???alert("值为: " + $("#test").val()); ?});
    回调函数。回调函数有两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。$("#btn1").click(function(){ ???$("#test1").text(function(i,origText){ ???????return "旧文本: " + origText + " 新文本: Hello world! (index: " + i + ")"; ????});});
  2. attr()和prop
  3. 用于获取属性值,对于元素自带的属性,用prop,对于我们自定义的属性,用attr。
    $("button").click(function(){ ?alert($("#runoob").attr("href"));});
    回调函数。回调函数有两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回您希望使用的字符串。$("button").click(function(){ ?$("#runoob").attr("href", function(i,origValue){ ???return origValue + "/jquery"; ??});});
  4. append(),prepend(),after(),before().
    append() - 在被选元素的结尾插入内容
    prepend() - 在被选元素的开头插入内容
    after() - 在被选元素之后插入内容
    before() - 在被选元素之前插入内容

    function appendText(){var txt1="<p>文本。</p>"; ?????????????// 使用 HTML 标签创建文本var txt2=$("<p></p>").text("文本。"); ?// 使用 jQuery 创建文本var txt3=document.createElement("p");txt3.innerHTML="文本。"; ??????????????// 使用 DOM 创建文本 text with DOM$("body").append(txt1,txt2,txt3); ???????// 追加新元素}function afterText(){var txt1="<b>I </b>"; ???????????????????// 使用 HTML 创建元素var txt2=$("<i></i>").text("love "); ????// 使用 jQuery 创建元素var txt3=document.createElement("big"); ?// 使用 DOM 创建元素txt3.innerHTML="jQuery!";$("img").after(txt1,txt2,txt3); ?
  5. remove()和empty().
    remove() - 删除被选元素(及其子元素)empty() - 从被选元素中删除子元素。

    $("#div1").remove()$("#div1").empty()下面的例子删除 class="italic" 的所有 <p> 元素$("p").remove(".italic")
  6. css类.
    addClass() - 向被选元素添加一个或多个类.
    removeClass() - 从选元素删除一个或多个类.
    toggleClass() - 对被选元素进行添加/删除类的切换操作.
    css() - 设置或返回样式属性.

    $("button").click(function(){ ?$("h1,h2,p").addClass("blue"); ?$("div").addClass("important");});
    $("button").click(function(){ ?$("body div:first").addClass("important blue");});
    下面的例子将展示如何使用 jQuery toggleClass() 方法。该方法对被选元素进行添加/删除类的切换操作:$("button").click(function(){ ?$("h1,h2,p").toggleClass("blue");})
    如需设置多个 CSS 属性,请使用如下语法:css({"propertyname":"value","propertyname":"value",...});$("p").css({"background-color":"yellow","font-size":"200%"});
  7. 尺寸.
    width(),height(),innerWidth(),innerHeight(),outerWidth(),outerHeight().
    下面的例子返回指定的 <div> 元素的宽度和高度:$("button").click(function(){ ?var txt=""; ?txt+="div 的宽度是: " + $("#div1").width() + "</br>"; ?txt+="div 的高度是: " + $("#div1").height(); ?$("#div1").html(txt);});下面的例子返回指定的 <div> 元素的 inner-width/height:$("button").click(function(){ ?var txt=""; ?txt+="div 宽度,包含内边距: " + $("#div1").innerWidth() + "</br>"; ???txt+="div 高度,包含内边距: " + $("#div1").innerHeight(); ?$("#div1").html(txt);});下面的例子返回指定的 <div> 元素的 outer-width/height:$("button").click(function(){ ?var txt=""; ?txt+="div 宽度,包含内边距和边框: " + $("#div1").outerWidth() + "</br>"; ?txt+="div 高度,包含内边距和边框: " + $("#div1").outerHeight(); ?$("#div1").html(txt);})

jQuery初学3(HTML)

原文地址:https://www.cnblogs.com/linnannan/p/9934918.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved