[英]Can't set css property on a button using JavaScript
I'm trying to add a button to my website to change the number of columns displayed, because it's a dictionary and more columns would be more efficient with space, but perhaps more difficult to navigate. 我正在尝试向我的网站添加一个按钮来更改显示的列数,因为它是一个字典,更多的列可以更有效地利用空间,但可能更难以导航。 I'm just using straight HTML, PHP, CSS, and just this little bit of javascript, but when I click the button, nothing happens.
我只是使用直接的HTML,PHP,CSS,以及这一点点的javascript,但是当我点击按钮时,没有任何反应。 I've tried what I can to fix it, but to no avail.
我已尽力解决它,但无济于事。 Here's the script (with CSS)
这是脚本(带CSS)
#col { -webkit-column-count: 2; -moz-column-count: 2; column-count: 2; }
<center><button onclick="thrcol()">Three Columns</button></center> <script type="text/javascript"> function thrcol() { document.getElementById("col").style.-webkit-column-count=3; document.getElementById("col").style.-moz-column-count=3; document.getElementById("col").style.column-count=3; } </script> <div id="col"><!-- A bunch of boring PHP that works -->Hello, World!<br>Hello, World!<br>Hello, World!</div>
I thought that this would just be simple, but, as I said, nothing happens. 我认为这很简单,但正如我所说,没有任何反应。 What's wrong here?
这有什么不对?
Your code was almost right, the only wrong thing was setting the style property. 你的代码几乎是正确的,唯一错误的是设置样式属性。 There are 2 ways to set a property to an object:
有两种方法可以将属性设置为对象:
object.property = 'value';
object['property'] = 'value';
The first way property
can contain only alphanumeric symbols and underscores. 第一种方式
property
只能包含字母数字符号和下划线。 The second way you can use (almost) every symbol. 第二种方式你可以使用(几乎)每个符号。
It is working now as expected: 现在按预期工作:
function thrcol() { document.getElementById("col").style.webkitColumnCount = 3; document.getElementById("col").style.mozColumnCount = 3; document.getElementById("col").style.columnCount = 3; // or: document.getElementById("col").style['-webkit-column-count'] = 3; document.getElementById("col").style['-moz-column-count'] = 3; document.getElementById("col").style['column-count'] = 3; }
button { display: block; } #col { -webkit-column-count: 2; -moz-column-count: 2; column-count: 2; }
<button onclick="thrcol()">Three Columns</button> <div id="col"> Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World!<br>Hello, World! </div>
Another way to set such dash-containing css properties using javascript: 使用javascript设置包含破折号的css属性的另一种方法:
"margin-left" becomes "marginLeft"
"border-bottom" becomes "borderBottom"
"-webkit-column-count" becomes "webkitColumnCount"
At first you must have document ready for setting event,then I think your css is not so correct.you can use this link.there are documentations and examples about html,css and javascript. 首先你必须有文件准备好设置事件,然后我认为你的CSS不是那么正确。你可以使用这个链接。有关于html,css和javascript的文档和示例。 http://www.w3docs.com/learn-javascript/javascript-events.html
http://www.w3docs.com/learn-javascript/javascript-events.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.