js 获取html css属性的值,html/css/js-如何利用jq来更改属性的值和获取属性的值

本文介绍了在jQuery中如何使用attr()和css()方法来获取和修改元素的属性值。attr()适用于标签属性,如id、name、title等,而css()则用于样式属性,如width、height。当尝试获取某些特定属性,如width时,使用attr()可能返回undefined,此时应改用css()。通过实例展示了正确使用这两个方法的方式,帮助开发者解决实际开发中遇到的问题。
摘要由CSDN通过智能技术生成

jquery的使用在web开发中是非常广泛的,虽然说比较容易,易学,但在开发过程中,也总是会碰到各种各样的小问题。

我曾经就遇到这种问题,jq如何获取属性值和更改属性值的。

众所周知,attr()可以更改属性值和获取属性值。

例如

alert($("#one").attr("name"));             这样可以获得id为one的控件的name属性的值,但是

alert($("#one").attr("width"));                                              提示框为undefined

出现上述现象的原因,是因为attr并不能获取所有的属性,虽然w3cschool上并没有讲,但我们学习上却碰到了这个问题。当然 attr() 无法获取某些属性值,但 css()却可以获取

如上改为

alert($("#one").css("width"));                        可以正确输出width的值

接下来,我来整理一下jq获取属性值的方法

方法一:attr()    (适用属性多为标签属性)

1.获取属性值:  $(selector).attr("属性名");

2.更改属性值:  $(selector).attr("属性名","属性值");

3适用属性: id

name

title

href

方法二: css()    (多为样式属性)

1.获取属性值: $(selector).css("属性名");

2.更改属性值:   $(selector).css("属性名","属性值");

3适用属性 : width,heigth,font-size,box-shadow,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我作为一名语言模型,无法为您提供完整的商城前端页面代码。不过,我可以为您提供一些基本的HTMLCSS、JavaScript和jQuery代码示例,以帮助您开始构建您的商城前端页面。 HTML代码示例: ``` <!DOCTYPE html> <html> <head> <title>My Online Store</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <h1>Welcome to My Online Store</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">Products</a></li> <li><a href="#">About Us</a></li> <li><a href="#">Contact Us</a></li> </ul> </nav> </header> <main> <section class="products"> <h2>Our Products</h2> <div class="product"> <img src="product1.jpg" alt="Product 1"> <h3>Product 1</h3> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <button class="buy">Buy Now</button> </div> <div class="product"> <img src="product2.jpg" alt="Product 2"> <h3>Product 2</h3> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <button class="buy">Buy Now</button> </div> <div class="product"> <img src="product3.jpg" alt="Product 3"> <h3>Product 3</h3> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <button class="buy">Buy Now</button> </div> </section> </main> <footer> <p>© 2021 My Online Store. All rights reserved.</p> </footer> <script src="script.js"></script> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </body> </html> ``` CSS代码示例: ``` * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: Arial, sans-serif; font-size: 16px; } header { background-color: #333; color: #fff; padding: 20px; text-align: center; } nav ul { list-style: none; margin: 0; padding: 0; } nav ul li { display: inline-block; } nav ul li a { color: #fff; display: block; padding: 10px; text-decoration: none; } main { padding: 20px; } .products { display: flex; flex-wrap: wrap; justify-content: space-between; } .product { background-color: #f2f2f2; border: 1px solid #ddd; margin-bottom: 20px; padding: 20px; text-align: center; width: calc(33.33% - 20px); } .product img { max-width: 100%; } .product h3 { margin-top: 10px; } .product p { margin-top: 10px; } .product button.buy { background-color: #333; border: none; color: #fff; cursor: pointer; margin-top: 10px; padding: 10px; transition: background-color 0.3s ease; } .product button.buy:hover { background-color: #555; } footer { background-color: #333; color: #fff; padding: 20px; text-align: center; } ``` JavaScript代码示例: ``` // Get all buy buttons const buyButtons = document.querySelectorAll('.buy'); // Handle buy button clicks buyButtons.forEach(button => { button.addEventListener('click', event => { alert('Product purchased!'); }); }); ``` jQuery代码示例: ``` // Get all buy buttons using jQuery const buyButtons = $('.buy'); // Handle buy button clicks using jQuery buyButtons.on('click', event => { alert('Product purchased!'); }); ``` 请注意,以上示例代码只是为了帮助您开始构建您的商城前端页面,并不是一个完整的解决方案。您需要根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值