我想将所选文本的颜色更改为#3C3,而不是div 2。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="lib/jquery.mobile-1.3.1.min.css" />
<script type="text/javascript" src="lib/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="lib/jquery.mobile-1.3.1.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" src="scripts/jquery.mobile-events.min.js"></script>
<script type="text/javascript" src="scripts/jquery.scrollTo-1.4.2-min.js"></script>
<link rel="stylesheet" href="css/test.css" />
<script type="text/javascript" src="scripts/jquery.xpath.js"></script>
<script type="text/javascript" src="scripts/test.js"></script>
</head>
<body>
<div data-role="page" id="test-page" class="bg_main">
<div data-role="header" id="bookmarkheader">.............. ...............</div>
<div data-role="content" id="content" class="content_bg">
<div class="one">some text</div>
<div class="two">some text</div>
</div>
<div data-role="footer" id="footer_main">................. .................</div>
</div>
</body>
</html>
CSS
.one::selection {
background-color:#3C3;
}
上面的代码没有用。有人可以帮帮我吗?
答案 0 :(得分:0)
如果您的移动浏览器不支持::selection
伪元素,那么您可以使用JavaScript来触发元素上的文本选择更改,并使用{{1}替换div中的所选文本用指定的颜色。
答案 1 :(得分:0)
以下是JQ方式DEMO http://jsfiddle.net/yeyene/GYuBv/6/
的方法$(function() {
$('div.one').mouseup( function() {
var mytext = selectHTML();
$('.one span').css({"color":"red"});
});
});
function selectHTML() {
try {
if (window.ActiveXObject) {
var c = document.selection.createRange();
return c.htmlText;
}
var nNd = document.createElement("span");
var w = getSelection().getRangeAt(0);
w.surroundContents(nNd);
return nNd.innerHTML;
} catch (e) {
if (window.ActiveXObject) {
return document.selection.createRange();
} else {
return getSelection();
}
}
}
<div data-role="page" id="index">
<div data-theme="b" data-role="header">
<h1>Index page</h1>
</div>
<div data-role="content">
<div class="one"><p>The quick brown fox jumps over the lazy dog</p></div>
<div class="two"><p>The quick brown fox jumps over the lazy dog</p></div>
</div>
</div>