项目临近上线,遇到了奇葩问题,ionic中的文字是无法像普通wap页面一样复制粘贴的。
翻了翻官方文档和中文网站,都没有对这个问题的说明。
后来经过多方查找资料,解决了这个问题。接下来分享给大家。
直接上代码:
html部分
Ionic文字复制问题ionic 测试copy
css部分
ion-content{
overflow-scroll: true;
}
.scroll-content {
-webkit-user-select: auto !important;
-moz-user-select: auto !important;
-ms-user-select: auto !important;
user-select: auto !important;
}
.selectable {
-webkit-user-select: auto;//控制网页内容选择范围
}
js部分
angular.module('ionicApp', ['ionic'])
.controller('MyCtrl', function($scope) {
stop_browser_behavior: false
self.touchStart = function(e) {
self.startCoordinates = getPointerCoordinates(e);
if ( ionic.tap.ignoreScrollStart(e) ) {
return;
}
if( ionic.tap.containsOrIsTextInput(e.target) ) {
// do not start if the target is a text input
// if there is a touchmove on this input, then we can start the scroll
self.__hasStarted = false;
return;
}
self.__isSelectable = true;
self.__enableScrollY = true;
self.__hasStarted = true;
self.doTouchStart(e.touches, e.timeStamp);
// e.preventDefault();
};
});
通过代码我们可以看到,首先在html中,添加overflow-scroll='true',然后在我们想要复制文字的容器上,添加自定义类,代码中我们添加的是'.selectable' ,在这个类上设置我们的css样式。
这里需要注意的是,这个自定义类,不能加在ionic的特定标签上。如下:
这样写,是无效的,我们必须这样写:
表示我就是因为这个没写对,调试了半天出不来效果。。。
最后一步就是在页面对应的controller里面拷贝如上js代码。
在这里附上实例demo,可以预览效果: