使用 onmousedown 来获取您刚刚按下鼠标的元素的 ID?

发布于 2024-08-01 22:51:36 字数 91 浏览 9 评论 0原文

这可能吗?

我正在尝试为 onmousedown 编写一个函数,它将返回您刚刚单击的元素的 ID,以便稍后在不同的 div 中重新创建该元素时使用。

Is this possible?

I am attempting to write a function for onmousedown that will return the ID of the element you just clicked for later use in recreating that element in a different div.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聆听风音 2024-08-08 22:51:36

您可以使用事件delegate,基本上只将一个事件处理程序连接到整个文档,并使用 event.target:

document.body.onmousedown = function (e) {
  e = e || window.event;
  var elementId = (e.target || e.srcElement).id;

  // call your re-create function
  recreate(elementId);
  // ...
}

function recreate (id) {
  // you can do the DOM manipulation here.
}

编辑: 您可以通过以下方式将事件分配给所有 Scriptaculous 拖动对象:

Event.observe(window, 'load', function () {
  Draggables.drags.each(function (item) {
    Event.observe(item.element, 'mousedown', function () {
      alert('mouseDown ' + this.id); // the this variable is the element 
    });                              // which has been "mouse downed"
  });
});

查看示例 此处

You can use event delegation, to basically connect only one event handler to your entire document, and get the element which the event was originally dispatched, using event.target:

document.body.onmousedown = function (e) {
  e = e || window.event;
  var elementId = (e.target || e.srcElement).id;

  // call your re-create function
  recreate(elementId);
  // ...
}

function recreate (id) {
  // you can do the DOM manipulation here.
}

Edit: You can assign events to all your Scriptaculous draggables in this way:

Event.observe(window, 'load', function () {
  Draggables.drags.each(function (item) {
    Event.observe(item.element, 'mousedown', function () {
      alert('mouseDown ' + this.id); // the this variable is the element 
    });                              // which has been "mouse downed"
  });
});

Check an example here.

汐鸠 2024-08-08 22:51:36

CMS 几乎有正确的答案,但您需要使其对跨浏览器更加友好。

document.body.onmousedown = function (e) {
  // Get IE event object
  e = e || window.event;
  // Get target in W3C browsers & IE
  var elementId = e.target ? e.target.id : e.srcElement.id;
  // ...
}

CMS pretty much has the correct answer but you will need to make it a little more cross browser friendly.

document.body.onmousedown = function (e) {
  // Get IE event object
  e = e || window.event;
  // Get target in W3C browsers & IE
  var elementId = e.target ? e.target.id : e.srcElement.id;
  // ...
}
你在我安 2024-08-08 22:51:36

请将此代码插入到您的 JavaScript 中。

document.getElementById("article").onmouseup(handMu);

Pls insert this code to your javascript.

document.getElementById("article").onmouseup(handMu);
温柔嚣张 2024-08-08 22:51:36

如果你想复制div id,一个简单的方法可能是像这样的cloneNode:

<div id="node1">
  <span>ChildNode</span>
  <span>ChildNode</span>
</div>

<div id="container"></div>

<script type="text/javascript">
  var node1 = document.getElementById('node1');
  var node2 = node1.cloneNode(true);

  node2.setAttribute('id', 'node2');

  var container = document.getElementById('container');
  container.appendChild(node2);
</script>

If you want to replicate the div id, an easy way might be cloneNode like this:

<div id="node1">
  <span>ChildNode</span>
  <span>ChildNode</span>
</div>

<div id="container"></div>

<script type="text/javascript">
  var node1 = document.getElementById('node1');
  var node2 = node1.cloneNode(true);

  node2.setAttribute('id', 'node2');

  var container = document.getElementById('container');
  container.appendChild(node2);
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文