- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个代码。选择文本时,将出现一个包含颜色的 div。单击其中一种颜色时,所选文本将包含该突出显示。但是,如何检查突出显示是否在另一个突出显示内部?如果突出显示位于另一个突出显示内部,我希望出现一个警告,表明此操作不可能。否则,将创建突出显示。
注意:您可以完全忽略 html 和 css 部分。
这是一个亮点中的亮点示例。我用黄色突出显示了短语There Hello There Hello There Hello
。然后,我在黄色荧光笔内突出显示e Hello There There Hello
。
$("#tooltip").mousedown(function(event) {
event.preventDefault();
});
//Only add the listener once, not another listener each mouseup
$(".boxes").click(function() {
var selection = document.getSelection();
var range = selection.getRangeAt(0);
var contents = range.extractContents();
var node = document.createElement("span");
node.classList.add($(this).attr("data-mark"));
node.appendChild(contents);
// Go ahead and create the span
range.insertNode(node);
selection.removeAllRanges(); //Clear the selection, showing highlight
hideTooltip();
});
function hideTooltip() {
document.getElementById('tooltip').style.display = ''; //hide the tooltip
}
$("#actual_verse").mouseup(function() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
if (/\S/.test(text)) {
// Tool Tip
var ele = document.getElementById('tooltip');
var sel = window.getSelection();
var rel1 = document.createRange();
rel1.selectNode(document.getElementById('cal1'));
var rel2 = document.createRange();
rel2.selectNode(document.getElementById('cal2'));
if (!sel.isCollapsed) {
var r = sel.getRangeAt(0).getBoundingClientRect();
var rb1 = rel1.getBoundingClientRect();
var rb2 = rel2.getBoundingClientRect();
//this will place ele below the selection
ele.style.top = (r.bottom - rb2.top) * 100 / (rb1.top - rb2.top) + 'px';
//this will align the right edges together
ele.style.left = (r.left - rb2.left) * 100 / (rb1.left - rb2.left) + 'px';
//code to set content
ele.style.display = 'block';
}
// End of Tool Tip
}
});
/* Highlighting */
.blue_mark {
background: #AAF6FF;
cursor: pointer;
}
.red_mark {
background: #FF9B9F;
cursor: pointer;
}
.green_mark {
background: #D6FFAA;
cursor: pointer;
}
.yellow_mark {
background: #FFF8AA;
cursor: pointer;
}
.orange_mark {
background: #FFBF98;
cursor: pointer;
}
.purple_mark {
background: #D7D5FC;
cursor: pointer;
}
/* End of Highlighting */
/* Tool Kit */
#tooltip {
position: absolute;
display: none;
border: grey solid 1px;
background: #373737;
padding: 5px;
border-radius: 3px;
}
#cal1 {
position: absolute;
height: 0px;
width: 0px;
top: 100px;
left: 100px;
overflow: none;
z-index: -100;
}
#cal2 {
position: absolute;
height: 0px;
width: 0px;
top: 0;
left: 0;
overflow: none;
z-index: -100;
}
.boxes {
width: 15px;
height: 15px;
cursor: pointer;
display: inline-block;
margin-right: 2px;
position: relative;
top: 3px;
}
#blue_box {
background: #AAF6FF;
}
#green_box {
background: #D6FFAA;
}
#orange_box {
background: #FFBF98;
}
#purple_box {
background: #D7D5FC;
}
#red_box {
background: #FF9B9F;
}
#yellow_box {
background: #FFF8AA;
}
.highlight {
background: yellow;
}
/* End of Tool Kit */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id='actual_verse' class='context'>Hello There Hello There Hello There Hello There! Hello There</span>
<div id='cal1'> </div>
<div id='cal2'> </div>
<div id='tooltip'>
<div id='blue_box' class='boxes' title='Blue' data-mark='blue_mark'></div>
<div id='green_box' class='boxes' title='Green' data-mark='green_mark'></div>
<div id='yellow_box' class='boxes' title='Yellow' data-mark='yellow_mark'></div>
<div id='orange_box' class='boxes' title='Orange' data-mark='orange_mark'></div>
<div id='purple_box' class='boxes' title='Purple' data-mark='purple_mark'></div>
<div id='red_box' class='boxes' title='Red' data-mark='red_mark'></div>
</div>
最佳答案
您可以强制选择在同一节点中开始和结束,并检查父节点:
var verse = document.getElementById("actual_verse");
if(range.startContainer !== range.endContainer ||
range.startContainer.parentElement !== verse ||
range.endContainer.parentElement !== verse
) {
alert("Not possible");
return;
}
var verse = document.getElementById("actual_verse");
$("#tooltip").mousedown(function(event) {
event.preventDefault();
});
//Only add the listener once, not another listener each mouseup
$(".boxes").click(function() {
var selection = document.getSelection();
var range = selection.getRangeAt(0);
if(range.startContainer !== range.endContainer ||
range.startContainer.parentElement !== verse ||
range.endContainer.parentElement !== verse
) {
alert("Not possible");
return;
}
var contents = range.extractContents();
var node = document.createElement("span");
node.classList.add($(this).attr("data-mark"));
node.appendChild(contents);
// Go ahead and create the span
range.insertNode(node);
selection.removeAllRanges(); //Clear the selection, showing highlight
hideTooltip();
});
function hideTooltip() {
document.getElementById('tooltip').style.display = ''; //hide the tooltip
}
$("#actual_verse").mouseup(function() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
if (/\S/.test(text)) {
// Tool Tip
var ele = document.getElementById('tooltip');
var sel = window.getSelection();
var rel1 = document.createRange();
rel1.selectNode(document.getElementById('cal1'));
var rel2 = document.createRange();
rel2.selectNode(document.getElementById('cal2'));
if (!sel.isCollapsed) {
var r = sel.getRangeAt(0).getBoundingClientRect();
var rb1 = rel1.getBoundingClientRect();
var rb2 = rel2.getBoundingClientRect();
//this will place ele below the selection
ele.style.top = (r.bottom - rb2.top) * 100 / (rb1.top - rb2.top) + 'px';
//this will align the right edges together
ele.style.left = (r.left - rb2.left) * 100 / (rb1.left - rb2.left) + 'px';
//code to set content
ele.style.display = 'block';
}
// End of Tool Tip
}
});
/* Highlighting */
.blue_mark {
background: #AAF6FF;
cursor: pointer;
}
.red_mark {
background: #FF9B9F;
cursor: pointer;
}
.green_mark {
background: #D6FFAA;
cursor: pointer;
}
.yellow_mark {
background: #FFF8AA;
cursor: pointer;
}
.orange_mark {
background: #FFBF98;
cursor: pointer;
}
.purple_mark {
background: #D7D5FC;
cursor: pointer;
}
/* End of Highlighting */
/* Tool Kit */
#tooltip {
position: absolute;
display: none;
border: grey solid 1px;
background: #373737;
padding: 5px;
border-radius: 3px;
}
#cal1 {
position: absolute;
height: 0px;
width: 0px;
top: 100px;
left: 100px;
overflow: none;
z-index: -100;
}
#cal2 {
position: absolute;
height: 0px;
width: 0px;
top: 0;
left: 0;
overflow: none;
z-index: -100;
}
.boxes {
width: 15px;
height: 15px;
cursor: pointer;
display: inline-block;
margin-right: 2px;
position: relative;
top: 3px;
}
#blue_box {
background: #AAF6FF;
}
#green_box {
background: #D6FFAA;
}
#orange_box {
background: #FFBF98;
}
#purple_box {
background: #D7D5FC;
}
#red_box {
background: #FF9B9F;
}
#yellow_box {
background: #FFF8AA;
}
.highlight {
background: yellow;
}
/* End of Tool Kit */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id='actual_verse' class='context'>Hello There Hello There Hello There Hello There! Hello There</span>
<div id='cal1'> </div>
<div id='cal2'> </div>
<div id='tooltip'>
<div id='blue_box' class='boxes' title='Blue' data-mark='blue_mark'></div>
<div id='green_box' class='boxes' title='Green' data-mark='green_mark'></div>
<div id='yellow_box' class='boxes' title='Yellow' data-mark='yellow_mark'></div>
<div id='orange_box' class='boxes' title='Orange' data-mark='orange_mark'></div>
<div id='purple_box' class='boxes' title='Purple' data-mark='purple_mark'></div>
<div id='red_box' class='boxes' title='Red' data-mark='red_mark'></div>
</div>
关于javascript - 使用 insertNode 防止跨度位于另一个跨度内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39301575/
这就是我现在正在做的事情。我在设计中使用 LESS CSS。我需要在指定输入之间放置 2 个跨度。所有元素的宽度都应为 100%。跨度应始终为 20px 宽度输入宽度可以根据屏幕宽度进行更改。谁能帮帮
我有一个包含文本和输入字段的跨度。我想知道是否可以让文本左对齐,输入字段右对齐。 NAME: .textBox{ display:inline-block; width:450px
我有这个按钮,我想让它在我点击“选择”时调用方法,在我点击“更改”时调用另一个方法: Select Change 我尝试输入 (click)="method()",但没有成功。我很
我正在开发聊天应用程序 (cordova),当我要在无法输入的表情符号后输入文本时,我在这段代码中遇到了问题 https://output.jsbin.com/radaref This i
很抱歉,如果这是一个非常愚蠢的问题,但我是一名开发人员,目前我的设计技能很少,我在个人网站上工作并且遇到了一个小问题。 我有一个带 ul 的顶部导航和 li元素。这些元素包含链接 . 跨度仅在链接悬
fiddle :https://jsfiddle.net/burz4g8s/4/ 我的 HTML 包含多行双按钮对。服务器端应用程序在 JSP 循环中输出按钮,所以我无法控制各个按钮——我不能使用 d
TextView.setLetterSpacing允许设置字母间距/字符间距。 有没有对应的CharacterStyle / span class允许在 TextView? 中的文本子集上设置字母间距
如何使洋红色矩形比红色矩形短 6 倍? GridLayout { id: gridLayout anchors.fill: parent flo
我最近开始使用 Twitter Bootstrap,但我似乎无法理解 span 的作用以及为什么会有不同的编号 span,例如 span4、span12?什么是偏移量以及它们何时使用? (有时与跨度一
我正在尝试构建一个 jQuery 函数来计算跨度中的总数 var sumnormaltotal = 0; $('span[id^="normaloffertotalspan"]').each(func
我想知道haskell如何评估以下表达式。 span (`elem` ['A'..'Z']) "BOBsidneyMORGANeddy" 结果是 ("BOB","sidneyMORGANeddy")
我有三个词,我想在第一个空格之后的内容周围添加一个跨度,所以 hello world 变成: hello world 和: hello world again 变成: hello world agai
我正在寻找纯 CSS 解决以下问题的方法。 考虑以下 HTML: Some text Some text 两者都是 正在显示元素 inline-block .如何在第二个 的左侧
如何将 Span 放置在其容器的底部? 我目前拥有的:http://jsfiddle.net/wRbax/2/ 我希望 .box 始终位于 .td 的底部 CSS .td { vertical
我试图在 li 中 float 两个 span。左跨度将有我的标签,在右跨度内我将构建一个具有嵌套跨度的图形。我有基本结构,但 chrome 将数字放在左侧跨度的末尾。我该如何解决这个问题? HTML
我有一个像这样的 JavaScript 变量: var text = "A businessman should be able to manage his business matters"; 我想
一些内容可编辑的框与其他框重叠,因此并非所有框都是可编辑的。我想保留与跨度位置中心对齐的文本,如下所示。我如何实现这一点? span { margin: auto; text-alig
我正在使用 WYSIWYG (InnovaEditor) 来编辑我网站上的内容,它适用于 Chrome、IE,主要适用于 Firefox,但 FF 有一个稍微令人讨厌的问题。我将 span 标签插入到
这是我的 html: Settings Export Import 和CSS: span.button { float:right; margin-righ
这里是问题所在:http://jsfiddle.net/STG22/3/ 我希望 span 不会分成两个不同的行(就像上面示例中的第三行一样)。我该怎么做? CSS: span { backg
我是一名优秀的程序员,十分优秀!