繁体   English   中英

无法用 jQuery 更改 CSS 样式

[英]Cannot change CSS style with jQuery

我正在从头开始学习 jQuery 和 JavaScript 并且无法理解为什么 css 不能与 ZF590B38FDA2C30C77BDDC3 一起添加。 基本上,我试图查看用户点击第一句话时句子是否加粗。 我一直在参考 w3school 和外部资源,但不确定为什么会出现错误。

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1:0"> <title>Question 3</title> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> </head> <body> <h3>Click on this heading to update below paragraph </h3> <p id="pText">this is a paragrah</p> <script> $(document).ready(function() { $("h3").click(function() { $('pText'),css("font-weight"; "bold"). document.getElementById('pText');innerHTML = "this is updated paragraph"; }); }); </script> </body> </html>

您错过了在选择器中添加 id 符号 ( # ) 的前缀:

$('#pText').css("font-weight", "bold");

我还建议您不要不必要地混淆香草 JS 和 jQuery。

你可以更换

document.getElementById('pText').innerHTML = "this is updated paragraph";

具有以下等效 jQuery:

$('#pText').html("this is updated paragraph");

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <body> <h3>Click on this heading to update below paragraph </h3> <p id="pText">this is a paragrah</p> <script> $(document).ready(function () { $("h3").click(function () { $('#pText').css("font-weight", "bold"); $('#pText').html("this is updated paragraph"); // you can use jQuery here }); }); </script> </body>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM