将 keyDown() 函数添加到 中这是 contenteditable div 的新子级
Jsfiddle: http://jsfiddle.net/qTEmc/1/
我需要将事件与按键关联添加到 contenteditable 中的链接的事件。
如果您尝试在链接的 jfiddle 中的可内容编辑区域中输入内容,您会看到它创建了一个链接,您可以在其中输入内容。如果你按回车键,你就会转到换行符。我想要的是在新链接中按回车键来触发函数。为了取得进展,我现在只是想让它返回警报。
有谁知道一个可靠的方法来做到这一点?
Jsfiddle: http://jsfiddle.net/qTEmc/1/
I need to associate events with the keypress event on links which are added in the contenteditable.
If you try typing in the contenteditable area in the linked jfiddle, you'll see it creates a link and you can type within it. I fyou press return, you go to a newline. What I want is for pressing return in the new link to trigger a function. For the sake of progress, I'm just trying to get it to return an alert at the moment.
Does anyone know a reliable way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将无法检测链接本身内的关键事件,因为它们不会触发关键事件。相反,您需要针对 contenteditable 元素调整现有的
keypress
处理程序,以检查所选内容是否位于链接内。这是一个可以做到这一点的函数。我还更新了您的演示。You won't be able to detect key events within the links themselves because they don't fire key events. Instead, you'll need to adapt your existing
keypress
handler for the contenteditable element to inspect the selection to see if it lies within a link. Here's a function to do that. I've also updated your demo.