创建带有锚点的文本选择的URL

问题描述 投票:2回答:5

我想创建一个指向我编写的网络服务的URL。http://mycoolthing.com,加上一个#-锚,指的是一个 选文 在该页面上。

是否可以在HTMLJS中创建一个指向Selection或RangeObject的锚?

javascript html text anchor selection
5个回答

1
投票

根据这个网站,你可以使用javascript来查找页面中的一些文字。

http:/www.javascripter.netfaqsearchin.htm

也许你可以创建一个链接到网站,并将文本选择用GET或POST包含在内,这样当其他页面加载时,你可以使用javascript和前一页的信息进入文本选择。

像这样在第一页中。

window.location.href="url?text=textToFind"

而在另一个页面上

$(function () {
window.find(window.location.search)
});

1
投票

正如我在评论中提到的,这里有一个选项,可以通过JavaScript和URL上的哈希(#)来选择文本。

它分为两部分。

  1. 调用页面将有一个链接到目标页面(它们可以是相同的),用#表示你要选择的文本(或者如果你知道它,元素的ID)。

    <a href="myPage.html#Lorem">Link to my page with Lorem select</a>

  2. 目标页面将有一些JavaScript代码,将选择#中指示的文本(如果哈希是一个有效的元素ID,它将选择该元素中的文本,而不是文本)。

*请注意,在这个解决方案中,我使用了一些来自于 选择元素中的文本(类似于用鼠标高亮)。 (Kimtho6的解决方案)

/**
 * SelectText: function from https://stackoverflow.com/questions/985272/selecting-text-in-an-element-akin-to-highlighting-with-your-mouse (Kimtho6 solution)
 * 
 * element: string that contains an element id (without the #)
 */
function SelectText(element) {
    var doc = document;
    var text = doc.getElementById(element);    
    if (doc.body.createTextRange) { // ms
        var range = doc.body.createTextRange();
        range.moveToElementText(text);
        range.select();
    } else if (window.getSelection) {
        var selection = window.getSelection();
        var range = doc.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);

    }
}

/**
 * selectHashText: function that selects the first appearance of a text (or an element with the id) indicated in the location hash
 */
function selectHashText() {

    // check if there's an element with that ID
    if ($(document.location.hash).length == 0) {

        // select some text in the page
        var textToSelect = document.location.hash.substring(1);

        // if it's not an empty hash
        if (textToSelect != "") {

            // iterate the different elements in the body
            $("body *").each(function() {

                // if one contains the desired text
                if ($(this).text().indexOf(textToSelect) >= 0) {

                    // wrap the text in a span with a specific ID
                    $(this).html(
                        $(this).html().replace(textToSelect, "<span id='mySelect'>" + textToSelect + "</span>")
                    );

                    // select the text in the specific ID and stop iteration
                    SelectText("mySelect");
                }
            });
        }

    } else {
        // select the content of the id in the page
        SelectText(document.location.hash.substring(1));
    }
}

// execute the text selection on page load and every time the hash changes
$(window).on("hashchange", selectHashText);
$(document).ready(function() { selectHashText(); });

我创造了这个 jsfiddle但它不取#,所以帮助不大。你也可以 在这个网页上看到. 请注意,这与jsfiddle(和上面)中的代码是一样的,只是在一个页面上。


更新: (根据下面的评论意见详细说明)

在不使用哈希的情况下,你可以将你要选择的文本作为参数传递到GET或POST中(为了简单起见,我将使用GET)。

<a href="myPage?select=Lorem">Select Lorem</a>

然后使用你的后端语言(我用的是PHP,你可以使用任何其他的语言,甚至是解析位置href的JavaScript),将文本保存到 textToSelect 变量,也就是我之前使用的变量。作为最后一步,将我之前使用的 .text() 对于 .html() 我使用的标签(所以标签也被包含在搜索中

/**
 * gup: function from https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-url-parameter (Haim Evgi's solution)
 * 
 * name: string that contains the name of the parameter to return
 */
function gup( name ) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    return results == null ? null : results[1];
}

/**
 * selectHashText: function that selects the first appearance of a text (or an element with the id) indicated in the "select" parameter
 */
function selectTextAcross() {

    // get the "select" parameter from the URL
    var textToSelect = decodeURIComponent(gup("select"));

    // if it's not an empty hash
    if (textToSelect != "") {

        // iterate the different elements in the body
        $("body *").each(function() {

            // if one contains the desired text
            if ($(this).html().indexOf(textToSelect) >= 0) {

                // wrap the text in a span with a specific ID
                $(this).html(
                    $(this).html().replace(textToSelect, "<span id='mySelect'>" + textToSelect + "</span>")
                );

                // select the text in the specific ID and stop iteration
                SelectText("mySelect");
            }
        });
    } 
}

$(document).ready(function() { selectTextAcross(); });

您可以 在这个网页上看到它的工作.

关于这个解决方案。

  • 它的工作原理(嘿!总比没有好! :P)
  • 你可以加入html标签(#号不能)
  • 你需要知道文件的来源
  • 生成的代码可能是无效的,然后结果可能不是预期的结果(我测试的所有浏览器,他们在第一个 "交叉 "标签后停止选择

0
投票

你看 https:/developer.mozilla.orgen-USdocsWebAPIdocument.execCommand。

创建链接

只有在有选择的情况下,才会从选择中创建一个锚链接。这需要传递HREF URI字符串作为值参数。URI必须至少包含一个字符,可以是一个空格。(Internet Explorer会用空的URI值创建一个链接。)

document.execCommand('createLink', false, 'http://google.com');

0
投票

你可以用以下方法获得选中的(高亮)文本。

var txt = window.getSelection().toString();

然后用该值建立一个锚

href="abc.html#' + txt;
© www.soinside.com 2019 - 2024. All rights reserved.