我试图找出如何在用户选择的文本中获取特定类的第一个和最后一个节点。例如,考虑以下HTML。几个范围被设置为“ fresca”类。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val data = arrayListOf<Department>(
Department(0, "Department1",
arrayListOf(
Item(0, "Item1.1"),
Item(1, "Item1.2"),
Item(3, "Item1.3")
)),
Department(0, "Department2",
arrayListOf(
Item(0, "Item2.1")
)),
Department(0, "Department3",
arrayListOf(
Item(0, "Item3.1"),
Item(1, "Item3.2")
)),
Department(0, "Department4",
arrayListOf(
Item(0, "Item4.1"),
Item(1, "Item4.2"),
Item(1, "Item4.3")
))
)
recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.adapter = Adapter(this, data)
}
}
class Adapter(val context: Context, val list: ArrayList<Department>): RecyclerView.Adapter<VH>() {
var total: Int = 0
var sectionIndices: ArrayList<Int> = arrayListOf()
var sectionSizes: ArrayList<Int> = arrayListOf()
init{
var index = 0
var pos = 0
for (d in list){
sectionIndices.add(pos)
sectionSizes.add(d.children.size)
pos += (1 + d.children.size)
index += 1
}
total = pos
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val view = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false)
return VH(view)
}
override fun getItemCount(): Int {
return total
}
override fun onBindViewHolder(holder: VH, position: Int) {
for (section in 0 .. sectionIndices.size - 1){
if (position >= sectionIndices.get(section)
&& position <= sectionIndices.get(section) + sectionSizes.get(section)){
val i = position - sectionIndices.get(section)
when (i){
0 -> {
// department
val department = list.get(section)
holder.textView.typeface = Typeface.DEFAULT_BOLD
holder.textView.text = department.title
}
else -> {
// item
val item = list.get(section).children.get(i - 1)
holder.textView.typeface = Typeface.DEFAULT
holder.textView.text = item.title
}
}
break
}
}
}
}
class VH(val view: View): RecyclerView.ViewHolder(view){
val textView: TextView
init {
textView = view.findViewById(android.R.id.text1)
}
}
data class Department(val id: Int, val title: String, val children: ArrayList<Item>)
data class Item(val id: Int, val title: String)
例如,如果用户选择“投手必须到达设定位置,他到达后将完全停止”,那么我的脚本将引用跨度“必须获得”以及跨度为“完全停止”。
如果用户选择“必须走脚”,那么我的脚本将获得两个跨度“右脚”的引用(或者可能只有一个引用)。
如果用户选择的内容不包含任何“ fresca”跨度,那么我的脚本只会得到一个null(或者可能是两个null组成的数组)。
答案 0 :(得分:0)
function getSelectionHtml() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
return html;
}
$(function() {
$(document).on("mouseup",'.select-area', function() {
// getting the selected html
var mytext = getSelectionHtml();
// parse it as html
myhtml = $.parseHTML(mytext);
// writing to temporary block
$('#tmp').html(myhtml);
// getting fresca elements
var elements = $('#tmp').find('.fresca');
var texts = new Array();
elements.each(function(){
// Pushing into texts
texts.push($(this).html());
})
console.log(texts);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select-area">
<p>
If he tries coming to first <span class="fresca">he will be called</span> for a
balk. Some pitchers will cross over their right knee but not cross
their right foot.
</p>
<p>
A pitcher <span class="fresca">must get</span> to a set position, where
he comes to a <span class="fresca">complete stop</span> after he gets
the sign but before he starts his motion home.
</p>
<p>
A pitcher's <span class="fresca">right foot</span>
must go in the general direction he is throwing.
</p>
</div>
<hr/>
<div style="display:none" id="tmp"></div>
借助this问题,我写了这个答案。试试吧。