正如我在评论中提到的,这是一个使用 JavaScript 和 url 上的哈希 (#) 来选择文本的选项。
分为两部分:
-
调用页面将有一个指向目标页面的链接(它们可以相同),并带有一个 # 表示您要选择的文本(或者如果您知道,则为元素的 ID):
<a href="myPage.html#Lorem">Link to my page with Lorem select</a>
目标页面将包含一些 JavaScript 代码,这些代码将选择 # 中指示的文本(如果哈希是有效的元素 ID,它将选择该元素中的文本而不是文本)。
*请注意,对于这个解决方案,我使用了来自 Selecting text in an element (akin to highlighting with your mouse) 的一些代码(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,但它不带#,所以它没有多大帮助。你也可以see it on this web page。请注意,这与 jsfiddle(及更高版本)中的代码相同,只是在一页上。
更新:(从下面的 cmets 详细阐述)
不使用哈希,您可以在 GET 或 POST 中将要选择的文本作为参数传递(为简单起见,我将使用 GET):
<a href="myPage?select=Lorem">Select Lorem</a>
然后使用您的后端语言(我使用 PHP,您可以使用任何其他甚至 JavaScript 解析位置 href),将文本保存到我之前使用的 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(); });
你可以see it working on this web page。
关于这个解决方案:
- 它有效(嘿!总比没有好!:P)
- 你可以包含html标签(你不能用#一)
- 您需要知道文档的来源
- 生成的代码可能无效,然后结果可能不是预期的(我测试的所有浏览器,它们在第一个“交叉”标记后停止选择)