extjs - 在 extjs 面板中获取选定文本很热门
问题描述
如何在extjs的面板中获取选定的文本?面板 html 属性包含一些格式化的 html 文本,但我想只获取纯文本,而不是格式化的 html 文本。那么,如果用户选择了一些文本,如何获得选择呢?
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.create('Ext.Container', {
renderTo: Ext.getBody(),
items: [{
xtype: 'panel',
title: 'my Panel',
html: "Some text. Let's say for the sake of this question: <i>Lorem ipsum dolor sit amet,...</i>" ,
width: 350
},
{
xtype: 'button',
text: 'my Button',
listeners:{
click: function(){
var selectedText=''; //THIS IS WHERE THE QUESTION IS
alert(selectedText);
}
}
}]
});
}
});
解决方案
您可以使用 window.getSelection().toString() 和 document.selection.createRange().text
Ext.application({
name: 'Fiddle',
launch: function () {
Ext.create('Ext.panel.Panel', {
renderTo: Ext.getBody(),
layout: {
type: 'hbox',
align: 'stretch'
},
defaults: {
height: 200,
flex: 1
},
items: [{
xtype: 'panel',
itemId: 'panelOne',
title: 'Panel One',
html: "Some <b>text</b>. Let's say for the sake of this question: <i>Lorem ipsum dolor sit amet,...</i>"
}, {
xtype: 'panel',
itemId: 'panelTwo',
title: 'Panel Two',
html: "Some <b>text</b>. Let's say for the sake of this question: <i>Lorem ipsum dolor sit amet,...</i>"
}],
buttons: [{
xtype: 'button',
text: 'my Button',
listeners: {
click: function () {
var selection = window.getSelection();
const panelItemIds = ['panelOne', 'panelTwo'];
panelItemIds.forEach(panelItemId => {
var panelComponent = this.up('panel').getComponent(panelItemId);
var node = selection.baseNode;
if (panelComponent) {
var panelComponentDom = panelComponent.getEl().dom;
while (node !== null) {
if (node == panelComponentDom) {
console.log(`Found selection "${selection.toString()}" in panel "${panelComponent.getTitle()}"`)
}
node = node.parentNode;
}
}
});
}
}
}]
});
}
});
推荐阅读
- ios - 删除 SwiftUI 表单填充
- android - 滚动动画到达顶部时折叠工具栏不展开
- pandas - 大熊猫给定条件的年度聚合
- pandas - 将一个数据帧中的列值映射到另一个数据帧的索引并提取值
- android - AAPT:错误:资源“attr/orientation”的重复值与配置“”
- ocaml - ocaml 列表中的最大 int
- react-native - 在本机反应中使用 opencv.js
- asp.net-core - 异常后 Application Insights RequestTelemetry 未显示在请求中
- node.js - PassportJS Google oauth2.0 登录/注册
- javascript - 从 easports.com 抓取数据,问题