如何通过所选文本获取关闭的父 div 属性?
How to get closed parent div attribute by selected text?
我有如下字符串。
<div data-sentence="1"><p>authorised representative (No. 45678) of</p></div>
<div data-sentence="2"><p>you have asked for my advice</p></div>
当用户select(绘制)像'45678'这样的字符串时,我可以通过下面的函数funGetSelectTxt 得到select编辑的文本。我的问题是如何让这个元素的父元素 div 和 return 返回属性值 'data-sentence'?
function funGetSelectTxt() {
var element = '';
if (document.selection) {
element = document.selection.createRange().text;
} else {
element = document.getSelection();
}
return $.trim(element.toString());
}
谢谢,
$(div).parent().attr('data-sentence');
和jQuery$(element).parent().attr('data-sentence');
在javascript
var child = document.getElementById("child");
var parent = child.parentNode;
var attr = parent.getAttribute("data-sentence");
我不知道为什么每个人仍然如此依赖jQuery,因为一些简单的任务更不容易写。
所以简单地说 javaScript,答案是:
element.parentNode.getAttribute('itemprop')
这样试试$(this).parent('div').attr('data-sentence')
$('div p').on('mouseup', funGetSelectTxt);
function funGetSelectTxt() {
var element = '';
if (document.selection) {
element = document.selection.createRange().text;
} else {
element = document.getSelection();
}
console.log(element.toString()) //its showing the selected text
console.log($(this).parent('div').attr('data-sentence')) //its get the attr value of parent
// return $.trim(element.toString());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-sentence="1"><p>authorised representative (No. 45678) of</p></div>
<div data-sentence="2"><p>you have asked for my advice</p></div>
<div data-sentence="1"><p>authorised representative (No. 45678) of</p></div>
<div data-sentence="2"><p>you have asked for my advice</p></div>
$('div p').on('click',function(){
console.log($(this).parent().attr('data-sentence'));
});
工作Link
我有如下字符串。
<div data-sentence="1"><p>authorised representative (No. 45678) of</p></div>
<div data-sentence="2"><p>you have asked for my advice</p></div>
当用户select(绘制)像'45678'这样的字符串时,我可以通过下面的函数funGetSelectTxt 得到select编辑的文本。我的问题是如何让这个元素的父元素 div 和 return 返回属性值 'data-sentence'?
function funGetSelectTxt() {
var element = '';
if (document.selection) {
element = document.selection.createRange().text;
} else {
element = document.getSelection();
}
return $.trim(element.toString());
}
谢谢,
$(div).parent().attr('data-sentence');
和jQuery$(element).parent().attr('data-sentence');
在javascript
var child = document.getElementById("child");
var parent = child.parentNode;
var attr = parent.getAttribute("data-sentence");
我不知道为什么每个人仍然如此依赖jQuery,因为一些简单的任务更不容易写。
所以简单地说 javaScript,答案是:
element.parentNode.getAttribute('itemprop')
这样试试$(this).parent('div').attr('data-sentence')
$('div p').on('mouseup', funGetSelectTxt);
function funGetSelectTxt() {
var element = '';
if (document.selection) {
element = document.selection.createRange().text;
} else {
element = document.getSelection();
}
console.log(element.toString()) //its showing the selected text
console.log($(this).parent('div').attr('data-sentence')) //its get the attr value of parent
// return $.trim(element.toString());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-sentence="1"><p>authorised representative (No. 45678) of</p></div>
<div data-sentence="2"><p>you have asked for my advice</p></div>
<div data-sentence="1"><p>authorised representative (No. 45678) of</p></div>
<div data-sentence="2"><p>you have asked for my advice</p></div>
$('div p').on('click',function(){
console.log($(this).parent().attr('data-sentence'));
});
工作Link