JavaScript 客户端宽度 : why is this returning undefined?

标签 javascript jquery undefined

我试图理解为什么它返回未定义。

function showMarquee(marquee_string) {
    $('#text-helper').html(marquee_string);
    var test = document.getElementById("text-helper").innerHTML;
    var width = (test.clientWidth); //why isn't this line working?
    alert(width);  //this returns undefined
}
showMarquee('Silver Linings Playbook');

HTML 如下:

<div class="currentwatchtext" id="currentwatchtext">
    <div id="marquee-screen"></div> 
    <div id="text-helper"></div>    
</div>

CSS 是:

#text-helper{
    position: absolute;
    visibility: hidden;
    height: auto;
    width: auto;
}

对于此事的任何意见都将受到赞赏。

最佳答案

.innerHTML 属性的值是一个字符串,而字符串没有“clientWidth”属性。

关于JavaScript 客户端宽度 : why is this returning undefined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21616338/

相关文章:

javascript - 扩展掌上电脑

javascript - Bootstrap 的 css 样式切换器

javascript - 使用 createUserWithEmailAndPassword 后 Firebase 无法看到用户

jquery - 为什么我可以在 jQuery 中使用 .hide() 但不能使用 .show()?

jQuery 改变 css 背景颜色

jquery - 选中单选按钮时显示一个 div,并将所需属性仅添加到输入字段

JavaScript 检查变量是否存在(已定义/初始化)

c - 调用内联函数时 undefined reference

javascript - 如何在一个 React 组件中输入数据并使其在另一个 React 组件中渲染?

javascript - 访问 JSON 对象中的数组元素未定义