设置innerHTML输出的样式
我正在尝试使用innerHTML函数创建一个JavaScript程序,其中用户在两个文本框中输入某人的名字和姓氏,然后JavaScript在单击按钮后按以下格式操作这些名称:
姓氏、名字。
我已成功让名称按该顺序显示“onclick”,但输出如下所示:
迪克
,
菲利普
.
我希望一切都在一条线上,不间断。我已经尝试创建一个会影响 div 的 css 类:然而,这不起作用。
到目前为止我正在使用的 HTML 看起来像这样:
<div id='displayName2'></div>,<div id='displayName1'></div>.
I'm trying to create a JavaScript program using the innerHTML function wherein the user types in the first and last names of somebody in two text-boxes and the the JavaScript manipulates the names after clicking a button to the following format:
Last Name, First Name.
I have successfully gotten the names to appear 'onclick' in that order, but the output looks like this:
Dick
,
Philip
.
I want everything to be on one line without breaks. I have already tried creating a css class that would affect the div: .nobr { white-space:nowrap; } This didn't work, however.
The HTML that I'm working with so far looks like this:
<div id='displayName2'></div>,<div id='displayName1'></div>.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要使这两个
标记显示且中间没有换行符,请使用 CSS 属性
display: inline;
或者使用< /code> 标签而不是
,因为
默认情况下内联显示,其中
是块元素。从语义上讲,
可能更合适。但如果您无法更改 HTML,请相应地修改您的 CSS:
To get those two
<div>
tags to appear without a linebreak in between, use the CSS attributedisplay: inline;
Or, use<span>
tags instead of<div>
, since<span>
is displayed inline by default, where<div>
is a block element. Semantically,<span>
is probably more appropriate., but if you cannot change the HTML, modify your CSS accordingly:为此,请使用
代替
。
如果您坚持使用
,请使用
display: inline
使呈现为内联元素。
Use
<span>
s instead of<div>
s for this purpose.If you insist on using
<div>
s, then usedisplay: inline
to make the<div>
s render as inline elements.最好使用
而不是
,它也具有中性语义,并且默认是内联的。
Better use
<span>
instead of<div>
, it also has neutral semantic meaning and it's inline by default.你有两个选择。
div
元素默认为display: block
。使用span
代替 div,或者使用display: inline
设置 div 的样式。You have 2 options.
div
elements aredisplay: block
by default. Either usespan
s instead of divs or style your divs usingdisplay: inline
.