java 将字体设为粗体,使用javascript将所选文字设为粗体

I have a text in my markup:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Vestibulum condimentum consectetur tellus, at bibendum felis ultrices eu.

Nullam nibh urna, euismod a blandit ut, fermentum a leo. Maecenas pharetra elementum fringilla.

Quisque condimentum, nibh quis elementum porttitor, magna libero malesuada dolor, ut feugiat tortor lectus ac turpis. Integer tristique molestie enim, sit amet commodo risus tempus non.

When user selects a text and presses CTRL+Enter I want to wrap the selected text with tags. I got to getting the selected text, but cannot find how I can wrap it with the markup. Here is what I have:

function getSelectedText () {

if (window.getSelection) {

return window.getSelection ().toString ();

}

else {

if (document.selection) {

return document.selection.createRange ().text;

}

}

return '';

}

$ (document).ready (function() {

// User pressed a key

$ (document).keydown (function(e) {

// is it CTRL+ENTER?

if (e.which == 13 && e.ctrlKey) {

alert('You have selected ' + getSelectedText ());

// now I need to highlight the text I got

// ????

}

});

});

Please note! A simple find/replace does not do, if a user selected a single 'a' letter which can be found 10 times in the text, I want to highlight the only 'a' he selected. I've studied range objects, but can't figure out how to achieve it, help me out, please.

Please see demo at jsfiddle.

解决方案

one of the examples is exactly what you're after.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值