How can I detect onclick() or similar for individual characters in a text?(如何检测文本中单个字符的 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() 或类似内容
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06