要将高度设置为等于JavaScript中的动态宽度,可以尝试运行以下代码-
<!DOCTYPE html> <html> <head> <style> .wrap { width:400px; height:300px; border:2px solid yellow; } .myChild{ width: 80%; border:1px solid red; } </style> <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"> </script> <script> var res = $('.myChild').width(); $('.myChild').css({ 'height': res + 'px' }); </script> </head> <body> <div class = "wrap"> <div class = "myChild"></div> </div> </body> </html>