改变属性的javascript代码
我正在尝试编写一段 JavaScript 代码,当特定事件发生时,该代码会更改 rect 的值。我正在思考 document.getElementById("xxx").setAttribute
.. 但我完全陷入困境。任何帮助将不胜感激!
我的html代码是这样的。
var x = document.getElementById("某个整数").value;
<div id="xxx" style="position: absolute; left: 500px; top: 520px; width: 220px; height: 10px; clip: rect(0pt, 10px, 10px, 0pt); background-color: rgb(0, 0, 255);">
我想将 rect 中的第二个值,即 (10px) 设置为 xpx。
I am trying to write a javascript code that would change the value of rect when a specific event occurs. I am thinking along the lines of document.getElementById("xxx").setAttribute
.. but I am completely stuck. Any help would be greatly appreciated!
The html code I have is this.
var x = document.getElementById("some integer").value;
<div id="xxx" style="position: absolute; left: 500px; top: 520px; width: 220px; height: 10px; clip: rect(0pt, 10px, 10px, 0pt); background-color: rgb(0, 0, 255);">
I want to set the second value in rect, ie (10px) to xpx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
document.getElementById("xxx").style.clip = "rect(10px, 10px, 10px, 10px)";
应该可以解决问题。clip
属性是 CSS 属性,因此需要使用 DOM 元素的style
属性来设置它。document.getElementById("xxx").style.clip = "rect(10px, 10px, 10px, 10px)";
should do the trick.The
clip
property is a CSS property, so you need to use thestyle
property of the DOM element to set it.这是我的一个建议..
或者尝试这个...
This is my one suggesion..
or try this...
您可以尝试这样的操作,以便可以更改任何/所有剪辑设置:
这样您就可以让任何点击事件提供您所需的值,例如 onclick="changeClipTo(0,50,10,0,'someElement' );”或 href="javascript:changeClipTo(0,50,10,0,'someElement');"
如果您事先不知道尺寸,那么您可以使用 onclick/href 调用一个确定所需值的函数,然后从该函数调用changeClipTo(),传入计算值。
You could try something like this, so that you can change any/all of the clip settings:
That way you can have any click event supply your desired values such as onclick="changeClipTo(0,50,10,0,'someElement');" or href="javascript:changeClipTo(0,50,10,0,'someElement');"
If you don't know the dimensions ahead of time, then you could use the onclick/href to call a function which determines the value you need, then call changeClipTo() from that function, passing in the computed value(s).