如何为jquery mouseup选择wrods包装html标签?
我在这里参考了答案用 getSelection 选择整个单词。我想从body
中进行mouseup
选择完成的单词
。而不是将
标签包装为
选择词
。这是代码。那么如何呢?谢谢。
<script src="jquery.js"></script>
<script>
(function($) {
function getSelected() {
if(window.getSelection) { return window.getSelection(); }
else if(document.getSelection) { return document.getSelection(); }
else {
var selection = document.selection && document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
return false;
}
function expand(range) {
if (range.collapsed) {
return;
}
while (range.toString()[0].match(/\w/)) {
range.setStart(range.startContainer, range.startOffset - 1);
}
while (range.toString()[range.toString().length - 1].match(/\w/)) {
range.setEnd(range.endContainer, range.endOffset + 1);
}
}
$(document).ready(function() {
$('body').mouseup(function() {
var selectionRange = getSelected().getRangeAt(0);
var start = selectionRange.startOffset;
expand(selectionRange);
var selection = selectionRange.toString();
if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
//how to wrap <p> tag for the selection words?
}
});
});
})(jQuery);
</script>
<style>
p {color:blue; }
</style>
<body>
Facebook needs to scrape your page to know how to display it around the site.
Facebook scrapes your page every 24 hours to ensure the properties are up to date. The page is also scraped when an admin for the Open Graph page clicks the Like button and when the URL is entered into the Facebook URL Linter. Facebook observes cache headers on your URLs - it will look at "Expires" and "Cache-Control" in order of preference. However, even if you specify a longer time, Facebook will scrape your page every 24 hours.
The user agent of the scraper is: "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
</body>
更新:
var new_html = $('body').html().split(selection, 1)[0] + '<p>' + selection + '</p>' + $('body').html().split(selection, 2)[1];
$('body').html(new_html);
I have reference the answer here Select whole word with getSelection. I would like to make a mouseup
selection completed words
from body
. than wrap <p>
tag for the selection words
. Here is the code. So how to? Thanks.
<script src="jquery.js"></script>
<script>
(function($) {
function getSelected() {
if(window.getSelection) { return window.getSelection(); }
else if(document.getSelection) { return document.getSelection(); }
else {
var selection = document.selection && document.selection.createRange();
if(selection.text) { return selection.text; }
return false;
}
return false;
}
function expand(range) {
if (range.collapsed) {
return;
}
while (range.toString()[0].match(/\w/)) {
range.setStart(range.startContainer, range.startOffset - 1);
}
while (range.toString()[range.toString().length - 1].match(/\w/)) {
range.setEnd(range.endContainer, range.endOffset + 1);
}
}
$(document).ready(function() {
$('body').mouseup(function() {
var selectionRange = getSelected().getRangeAt(0);
var start = selectionRange.startOffset;
expand(selectionRange);
var selection = selectionRange.toString();
if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
//how to wrap <p> tag for the selection words?
}
});
});
})(jQuery);
</script>
<style>
p {color:blue; }
</style>
<body>
Facebook needs to scrape your page to know how to display it around the site.
Facebook scrapes your page every 24 hours to ensure the properties are up to date. The page is also scraped when an admin for the Open Graph page clicks the Like button and when the URL is entered into the Facebook URL Linter. Facebook observes cache headers on your URLs - it will look at "Expires" and "Cache-Control" in order of preference. However, even if you specify a longer time, Facebook will scrape your page every 24 hours.
The user agent of the scraper is: "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)"
</body>
UPDATE:
var new_html = $('body').html().split(selection, 1)[0] + '<p>' + selection + '</p>' + $('body').html().split(selection, 2)[1];
$('body').html(new_html);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
http://jsfiddle.net/CHhJG/
Try this:
http://jsfiddle.net/CHhJG/