简体   繁体   English

通过Javascript设置段落CSS宽度无效

[英]Setting a paragraphs CSS width through Javascript doesn't work

In Javascript, I have a div with a paragraph inside it. 在Javascript中,我有一个div,其中有一个段落。 I have made an effect where the div slowly expands in width & reveals the text in the paragraph below. 我的效果是div的宽度逐渐扩大并显示以下段落中的文本。

I set the divs width to zero, then every 10 milliseconds I increase the width by 10px. 我将divs宽度设置为零,然后每10毫秒将宽度增加10px。 This works great because I have set the div to have overflow hidden. 这很棒,因为我将div设置为隐藏了溢出。

My Problem: BUT the text in the paragraph wraps to the current width of the parent div, which means that the text jumps around & reformats as the width increases. 我的问题:但是段落中的文本会换成父div的当前宽度,这意味着文本会随着宽度的增加而跳动并重新格式化。

I want to remove/stop this from occuring so I explicity set the paragraphs width to 100px (the width of the div once it has completely expanded) but the problem is when I go check the CSS width of the paragraph element in Firebug, its not set, ie its not listed in the HTML elements inline CSS style? 我想删除/阻止这种情况的发生,所以我明确地将段落宽度设置为100px(一旦完全扩展,div的宽度),但是问题是当我去检查Firebug中段落元素的CSS宽度时,它没有设置,即其未在HTML元素内联CSS样式中列出?

It makes me think that a paragraph element by default has display block & ignores the width parameter, is that correct? 这让我认为默认情况下一个段落元素具有显示块并且忽略width参数,对吗?

My ultimate question is: How can I get the paragraph element to be 100px wide? 我的最终问题是:如何获得100px的段落元素? Do I need to change the display type to get the width to work? 我需要更改显示类型以使宽度起作用吗?

This doesn't work: 这不起作用:

pEle.style.width = "100px"; pEle.style.width =“ 100px”; // maybe I need to change the display type before I set its width? //也许我需要在设置宽度之前更改显示类型?

If I understand your question correctly you can do this just be explicitly setting a width for the paragraph in your CSS. 如果我正确理解了您的问题,则可以在CSS中显式设置段落的宽度。

By default a paragraph element is block level and will take take any width you specify... Leads me to think somethings going wrong with how you're trying set the width. 默认情况下,段落元素是块级的,它将采用您指定的任何宽度...让我觉得您尝试设置宽度的方式出了一些问题。

Here's a quick fiddle - http://jsfiddle.net/MerlinMason/Sdq65/ 这是一个快速的小提琴-http: //jsfiddle.net/MerlinMason/Sdq65/

Hope that helps! 希望有帮助!

我真的相信,可以通过将以下CSS属性添加到您的段落中来解决所有问题:

white-space: nowrap;

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

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