如何检测文本中单个字符的 onclick() 或类似内容

How can I detect onclick() or similar for individual characters in a text?(如何检测文本中单个字符的 onclick() 或类似内容?)

本文介绍了如何检测文本中单个字符的 onclick() 或类似内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Javascript 新手,想通过单击单个字符来修改文本字符串.字符串是:0000 0000 0000 0000 代表一个二进制数.我希望能够通过直接单击文本将 0 切换为 1.

I'm new to Javascript and would like to modify a text string by clicking on individual characters. The string is: 0000 0000 0000 0000 representing a binary number. I would like to be able to toggle a 0 to a 1 by clicking directly on the text.

我曾尝试使用 onclick() 但只成功检测到整个段落的点击.检测单击哪个字符的合适方法是什么?

I have tried to use onclick() but have only managed to detect a click for the entire paragraph. What would be an appropriate method to detect which character is clicked?

推荐答案

对于这么少的字符,最简单的方法是把每个字符放在自己的 span 中:

For such a small number of characters, the easiest way is to put each of them in its own span:

<span>0</span><span>0</span><span>0</span><span>0</span> <span>0</span><span>0</span><span>0</span><span>0</span> <span>0</span><span>0</span><span>0</span><span>0</span>

我还将所有这些都放在一个容器中,并将 click 事件挂在容器上而不是单个跨度上,所以:

I'd also put all of those in a container, and hook the click event on the container rather than on the individual spans, so:

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

然后把它挂起来:

var container = document.getElementById("container");
if (container.addEventListener) {
    container.addEventListener('click', clickHandler, false);
}
else if (container.attachEvent) {
    container.attachEvent('onclick', function(e) {
        return clickHandler.call(container, e || window.event);
    });
}

在您的点击处理程序中,使用 event.target 找出点击了哪个跨度:

In your click handler, use event.target to find out which span was clicked:

function clickHandler(event) {
    var span = event.target;
    // Do something with the span, such as look at its `innerHTML` and
    // see if it's "0" -- if so, make it "1"; if not, make it "0"
}

更多探索:

  • DOM2 规范
  • DOM3 规范
  • DOM2 HTML
  • HTML5 Web 应用程序 API

正如您在上面看到的,我不得不解决一些浏览器使用标准 addEventListener,而其他浏览器(IE8 和更早版本)使用 attachEvent 的事实.我建议使用一个好的 JavaScript 库,例如 jQuery、原型、YUI、关闭,或其他任何一个.它们为您消除了这些浏览器的不一致,并添加了许多非常有用的实用程序功能,因此您可以专注于您正在尝试做的事情.

As you can see above, I had to work around the fact that some browsers use the standard addEventListener, and others (IE8 and earlier) use attachEvent. I recommend using a good JavaScript library like jQuery, Prototype, YUI, Closure, or any of several others. They smooth over those kinds of browser inconsistencies for you, and add a lot of very useful utility functionality so you can focus just on what you're trying to do.

例如,使用 jQuery 编写的处理程序代码:

For example, that handler code written using jQuery:

$("#container").on("click", "span", function() {
    // `this` refers to the span that was clicked; you can use
    // `innerHTML` as above, or wrap it in a jQuery instance
    // like this:
    //    var $this = $(this);
    // ...and then use jQuery's `html` function to both
    // retrieve and set the HTML.
});

这篇关于如何检测文本中单个字符的 onclick() 或类似内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何检测文本中单个字符的 onclick() 或类似内容