单击单词出现的返回句 [英] Return Sentence That A Clicked Word Appears In

查看:85
本文介绍了单击单词出现的返回句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循上一个问题:使用Javascript获得点击单词的句子

我一直在摸索这个问题已有一段时间了.但是,我今天早上醒来,开始阅读以下内容: http://branch.com/b/nba-playoffs-round-1

I have been fumbling around with this question for some time now. However, I woke up this morning and started reading this: http://branch.com/b/nba-playoffs-round-1

Voila!分支允许用户选择一个句子,然后共享它,保存它,等等.这正是我想要做的.好像他们将每个句子都包装在<span>标记中.

Voila! Branch allow for users to select a sentence and then share it, save it, etc...That's exactly what I want to do. It looks like they're wrapping each sentence in <span> tags.

以前,人们建议找到每个<p>标签,然后将标签中的每个句子分开.但是,我正在做一个chrome扩展程序,它实际上需要在任何网站上都可以使用,因此单词可能会出现在<p>标记之外,可能会出现在<h1>类型的标记中,甚至可能出现在<div>中.

Previously, people have suggested to find each <p> tag and then break each sentence up within the tag. However, I am making a chrome extension and this needs to work on virtually any website, so a word could appear outside a <p> tag, maybe in an <h1> type tag or even in a <div>.

对Branch的工作有何见解?

Any insight into how Branch did it?

推荐答案

您可以做这样的事情,与您追求的目标不太一样,我认为呢?但可能会为您提供进一步的想法.

You can do something like this, not quite the same as what you are after, I think? But may give you a start to further ideas.

<div>In cryptography, a keyed-hash message authentication code (HMAC) is a specific construction for calculating a message authentication code (MAC) involving a cryptographic hash function in combination with a secret cryptographic key. As with any MAC, it may be used to simultaneously verify both the data integrity and the authentication of a message. Any cryptographic hash function, such as MD5 or SHA-1, may be used in the calculation of an HMAC; the resulting MAC algorithm is termed HMAC-MD5 or HMAC-SHA1 accordingly. The cryptographic strength of the HMAC depends upon the cryptographic strength of the underlying hash function, the size of its hash output, and on the size and quality of the key.</div>
<button id="get">Get Selected</button>

function getText() {
    var selectedText

    if (typeof window.getSelection === "function") {
        selectedText = window.getSelection();
    } else if (typeof document.getSelection === "function") {
        selectedText = document.getSelection();
    } else if (document.selection && typeof document.selection.createRange() === "function") {
        selectedText = document.selection.createRange().text;
    } else {
        selectedText = "";
        alert("No method to get selected text");
    }

    if (!selectedText || selectedText === "") {
        if (document.activeElement.selectionStart) {
            selectedText = document.activeElement.value.substring(
            document.activeElement.selectionStart.document.activeElement.selectionEnd);
        }
    }

    alert(selectedText);
}

document.getElementById("get").addEventListener("click", getText, false);

jsfiddle

您还可以在我对该想法进行扩展的地方看到进一步的答案

you can also see a further answer where I have expanded on this idea here on SO.

作者提出了另一个问题,但这是另一个jsfiddle

the author pulled the other question but here is the other jsfiddle

window.getSelection

摘要

返回一个选择对象,该对象表示由选定的文本范围 用户.

Returns a selection object representing the range of text selected by the user.

规格

DOM级别0.不属于任何标准.

DOM Level 0. Not part of any standard.

预计将在新的DOM Range规范中指定

It is expected to be specified in a new DOM Range spec

还有一个名为 Rangy 的库,应该可以处理这种类型的瘦跨浏览器,从未尝试过,但是您可能想看看.

There is also a library called Rangy that is supposed to handle this kind of thin cross-browser, never tried it but you may want to take a look.

跨浏览器的JavaScript范围和选择库.它提供了一个 简单的基于标准的API,用于执行常见的DOM Range和 所有主要浏览器中的选择任务,疯狂地抽象出来 Internet之间此功能的不同实现 资源管理器,最高包括版本8和与DOM兼容的浏览器.

A cross-browser JavaScript range and selection library. It provides a simple standards-based API for performing common DOM Range and Selection tasks in all major browsers, abstracting away the wildly different implementations of this functionality between Internet Explorer up to and including version 8 and DOM-compliant browsers.

这篇关于单击单词出现的返回句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆