欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > HTML >内容正文

HTML

前端页面:一直报Cannot set property 'height' of undefined

发布时间:2025/3/8 HTML 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 前端页面:一直报Cannot set property 'height' of undefined 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1、出现错误的例子,只拷贝了项目中关键出现问题的部分

例子中明明写了style='height:16px'这个属性,但是为什么还说height未定义呢

通过打印发现:cks.each(function () {
            autoTextAreaHeight($(this));
          });中的$(this)取出来被当成数组处理了,所以要加上[0]

<!DOCTYPE html> <html> <head><meta charset="utf-8"><script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script><title>菜鸟教程(runoob.com)</title><script>var element = "<span>" +"<input type='checkbox' value='" + 0 + "'>" +"<textarea style='height:16px; width:100px;margin-left:7px;' class='show_text' oninput='autoTextAreaHeight(this)' "+">asdsaaaaaaaaaaaaawqdqwwqa</textarea>"//$.find("body").append(element);$(window).load(function(){ //用jq在body中动态添加元素$(document.body).append(element);$(document.body).append(element);//初始化调用文本域高度自适应方法$(function () {var cks = $(".show_text");cks.each(function () {autoTextAreaHeight($(this));})}) });//文本域自适应function autoTextAreaHeight(o) {o.style.height = o.scrollTop + o.scrollHeight + "px";} </script> </head> <body> </body> </html>修改成

cks.each(function () {

autoTextAreaHeight($(this)[0]);

})

创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖

总结

以上是生活随笔为你收集整理的前端页面:一直报Cannot set property 'height' of undefined的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。