分享web开发知识

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

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

jquery如何为元素设置style?

发布时间:2023-09-06 02:02责任编辑:白小东关键词:style
$("#userLevelCss").attr("style","width:78%;float: right;display: none;");

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

记录一下用JavaScript原生代码 以及jQuery 设置/获取 属性的方法:

(文章最下面有完整版代码)

首先把JavaScript的奉上

function attribute() { ???????var val=document.getElementById("in1").value, ???????????a1=document.getElementById("a1"), ???????????d2=document.getElementById("d2"); ???????//第一种直接设置本身自有属性方法 ???????a1.href="https://www."+val+".com"; ???????//第二种设置自定义属性方法 ???????d2.setAttribute("url","https://www."+val+".com"); ???????//获取选中属性的值 ???????var d1Url=d1.getAttribute("url"); ???????console.log(d1Url); ???????//设置样式 ???????d2.style.background="#FAB2C9"; ???}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

运行结果如下:


再来就是jQuery的

 ???????//设置属性、值 ???????$("#a2").attr("href","http://www.w3school.com.cn/"); ???????//同时设定多个 ???????$("#a2").attr({ ???????????"data-num":"50", ???????????"target":"view_window" ???????}); ???????//获取选择属性的值: ???????var a2Href=$("#a2").attr("href"); ???????console.log("a2链接地址为:"+a2Href); ???????//设定样式 ???????$("#d2").css("border","5px solid #8E00FF"); ???????//同时设定多个 ???????$("#d2").css({ ???????????"width" : "200", ???????????"height" : "50", ???????????"background":"#E058EA" ???????});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

运行结果如下:


整篇代码

<!DOCTYPE html><html><head lang="en"> ???<meta charset="UTF-8"> ???<title></title> ???<style> ???????#d1{ ???????????width:200px;height:50px; ???????} ???</style></head><body><div> ???<h3>JavaScript</h3> ???<input type="text" id="in1" ?value="baidu"/> ???<div id="d1"></div> ???<a href="#" id="a1">超链接</a></div><hr><div> ???<h3>jQuery</h3> ???<a href="#" id="a2">点我跳转</a> ???<div id="d2"></div></div><script> ???function attribute() { ???????var val=document.getElementById("in1").value,a1=document.getElementById("a1"),d1=document.getElementById("d1"); ???????//第一种设置本身自有属性方法 ???????a1.href="https://www."+val+".com"; ???????//第二种设置自定义属性方法 ???????d1.setAttribute("url","https://www."+val+".com"); ???????//获取选中属性的值 ???????var d1Url=d1.getAttribute("url"); ???????console.log(d1Url); ???????//设置样式 ???????d1.style.background="#FAB2C9"; ???} ???attribute();</script><script src="jquery-1.12.4.min.js"></script><script> ???????//设置属性、值 ???????$("#a2").attr("href","http://www.w3school.com.cn/"); ???????//同时设定多个 ???????$("#a2").attr({ ???????????"data-num":"50", ???????????"target":"view_window" ???????}); ???????//获取选择属性的值: ???????var a2Href=$("#a2").attr("href"); ???????console.log("a2链接地址为:"+a2Href); ???????//设定样式 ???????$("#d2").css("border","5px solid #8E00FF"); ???????//同时设定多个 ???????$("#d2").css({ ???????????"width" : "200", ???????????"height" : "50", ???????????"background":"#E058EA" ???????});</script></body></html>

jquery如何为元素设置style?

原文地址:https://www.cnblogs.com/YuyuanNo1/p/9258988.html

知识推荐

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