简体   繁体   English

在HTML表格上使用Javascript?

[英]Using Javascript on an HTML table?

A few questions, just wondering if anyone can help? 几个问题,只是想知道是否有人可以提供帮助?

I have a table with ONE long row (1000 pixels) and one single column, how do I go about creating a method whereby when the mouse cursor is on the leftmost side of the cell, a variable, lets say X is set to 0, the further right the mouse cursor moves in the cell, the value of X increases. 我有一张长一行(1000像素)和一列的表格,我该如何创建一种方法,当鼠标光标位于单元格的最左侧时,将一个变量设为X设置为0,鼠标光标在单元格中移动得越靠右,X的值就会增加。

I know that sounds like a strange question but I'm working on a project where this type of functionality is desired. 我知道这听起来像是一个奇怪的问题,但是我正在一个需要这种功能的项目中工作。

Is there a Javascript method to create this feature? 是否有Javascript方法可创建此功能?

Is there a javascript method to create this feature? 有没有使用javascript方法创建此功能的方法?

Yes, there are things in JavaScript which can do what you want. 是的,JavaScript中有些事情可以做您想要的。 Basically you need to put an "onmousemove" event on your table. 基本上,您需要在表上放置一个“ onmousemove”事件。 The following will work for Firefox and Chrome: 以下内容适用于Firefox和Chrome:

<html>
<body>
<script>
function movedamouse (event)
{
var mytd = document.getElementById ("mytd");
myxpos = event.pageX;
mytext = document.createTextNode (myxpos);
while (mytd.firstChild)
mytd.removeChild (mytd.firstChild);
mytd.appendChild (mytext);
}
</script>
<table>
<tr>
<td>
bbaby
</td>
<td id="mytd" onmousemove="movedamouse(event)"
    style="width:1000px;background:chartreuse">
</td>
</tr>
</table>
</body>
</html>

I'll leave fixing this for Internet Explorer as an exercise. 作为练习,我将为Internet Explorer修复此问题。 Also, the offset bit needs some work. 同样,偏移位需要一些工作。 Anyway this is just to illustrate the idea. 无论如何,这仅仅是为了说明这个想法。

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

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