如何包装html标签为jquery鼠标选择单词
How to wrap html tag for jquery mouseup selection wrods?
我在这里引用了答案,使用getSelection选择整个单词。我想让mouseup
从body
中选择completed words
。然后为selection words
包装<p>
标签。下面是代码。那么该怎么做呢?谢谢。
<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);
试试这个:
(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,''))) {
var el = $('body');
el.html(el.html().replace(selection, "<p>"+selection+"</p>")); }
});
});
})(jQuery);
http://jsfiddle.net/CHhJG/相关文章:
- jquery鼠标选择无法正常工作
- jQuery鼠标输出调用CSS3动画
- jQuery鼠标悬停渐变效果
- Javascript/jQuery-鼠标事件没有在html上触发,添加了动态
- 我想要一个类似于网站 https://onlycoin.com/ 的jQuery鼠标滚动动画效果
- 正在尝试将jquery鼠标滚轮插件插入到jquery循环2中
- Jquery鼠标滚轮与触控板水平滚动
- 如果鼠标悬停得很快,Jquery鼠标移动会错过一些单元格
- jQuery-鼠标输入闪烁图像
- 带有setInterval的JQuery鼠标
- 如何清除jQuery鼠标悬停#id上的setTimeout
- jQuery鼠标滚动到下一个id
- Jquery鼠标悬停在子项上触发
- JQuery 鼠标悬停/淡出与淡入淡出影响单击与淡出
- 使用 jquery 鼠标滚轮
- Jquery 鼠标悬停和鼠标离开
- jQuery - 鼠标移动移动移动
- jQuery 鼠标滚轮插件缩放问题
- jQuery 鼠标事件不会触发
- 使用 jQuery 鼠标悬停时的 Svg 颜色过渡