- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
This is my jquery code that I am using to truncate the pasted text, so that it doesn't exceed the maxlength of an element. The default behaviour on Chrome is to check this automatically but in IE 8 and 9 it pastes the whole text and doesn't check the maxLength of an element. Please help me to do this. This is my first time asking a question here, so please let me know if I need to provide some more details. Thanks.
<script type="text/javascript">
//var lenGlobal;
var maxLength;
function doKeypress(control) {
maxLength = control.attributes["maxLength"].value;
value = control.value;
if (maxLength && value.length > maxLength - 1) {
event.returnValue = false;
maxLength = parseInt(maxLength);
}
}
//function doBeforePaste(control) {
//maxLength = control.attributes["maxLength"].value;
//if (maxLength) {
// event.returnValue = false;
//var v = control.value;
//lenGlobal = v.length;
// }
// }
$(document).on("focus","input[type=text],textarea",function(e){
var t = e.target;
maxLength = parseInt($(this).attr('maxLength'));
if(!$(t).data("EventListenerSet")){
//get length of field before paste
var keyup = function(){
$(this).data("lastLength",$(this).val().length);
};
$(t).data("lastLength", $(t).val().length);
//catch paste event
var paste = function(){
$(this).data("paste",1);//Opera 11.11+
};
//process modified data, if paste occured
var func = function(){
if($(this).data("paste")){
var dat = this.value.substr($(this).data("lastLength"));
//alert(this.value.substr($(this).data("lastLength")));
// alert(dat.substr(0,4));
$(this).data("paste",0);
//this.value = this.value.substr(0,$(this).data("lastLength"));
$(t).data("lastLength", $(t).val().length);
if (dat == ""){
this.value = $(t).val();
}
else
{
this.value = dat.substr(0,maxLength);
}
}
};
if(window.addEventListener) {
t.addEventListener('keyup', keyup, false);
t.addEventListener('paste', paste, false);
t.addEventListener('input', func, false);
} else{//IE
t.attachEvent('onkeyup', function() {keyup.call(t);});
t.attachEvent('onpaste', function() {paste.call(t);});
t.attachEvent('onpropertychange', function() {func.call(t);});
}
$(t).data("EventListenerSet",1);
}
});
</script>
最佳答案
你可以做这样的事情,请注意,这是在 YUI 中完成的,但可以为 jquery 做类似的事情。您需要做的就是获取输入的评论的长度,然后将文本 chop 到所需的长度,在本例中为 2000 个字符。
comment_text_box.on('valuechange', function(e) {
//Get the comment the user input
var comment_text = e.currentTarget.get('value');
//Get the comment length
var comment_length = comment_text.length;
if(comment_length > 2000){
alert('The comment entered is ' + comment_length + ' characters long and will be truncated to 2000 characters.');
//Truncate the comment
var new_comment = comment_text.substring(0, 2000);
//Set the value of the textarea to truncated comment
e.currentTarget.set('value', new_comment);
}
});
关于javascript - 如何在 chrome、IE 8 和 9 中使用 jquery 操作剪贴板数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20201909/
当尝试复制到剪贴板时,有什么区别 Clipboard.SetData(DataFormats.Text, ""); 和 Clipboard.SetText(""); 最佳答案 SetText 只是 S
我正在尝试将一个对象复制到 Windows 剪贴板上,然后再次关闭。我的代码是这样的: 复制到剪贴板: Clipboard.Clear(); DataObject newObject = new Da
如何在非静态线程中获取剪贴板文本?我有一个解决方案,但我正在尝试获得最干净/最短的方法。正常调用时结果为空字符串。 最佳答案 我会添加一个辅助方法,它可以在 MTA 主线程中将 Action 作为 S
我正在研究Applescript,将上次复制的内容粘贴到任何当前字段中。它将与VoiceOver一起使用,并且关键代码方式(我所知道的唯一方式)并非始终有效。 tell application
我在这里看到: http://www.pgrs.net/2008/1/11/command-line-clipboard-access Linux 和 osx 中有一种方法可以从命令行复制到剪贴板。所
我正在尝试使用已在浏览器中运行的 Clipboard API,但 PhpStorm 不知道它。 怎样才能让 PhpStorm 识别它?我已在项目设置中将 JavaScript 语言版本设置为 ECMA
Wayland 中有剪贴板 API 之类的东西吗?或者我应该在哪里以编程方式将内容粘贴到剪贴板? 我在 Wayland 上运行 Fedora 24。 如果我觉得有一个剪贴板完全没问题,那么有代码示例(
我最近看到一个针对 ClearCase 的绝妙 hack,其中版本号作为提交的一部分被添加到 Windows 剪贴板。黑客看起来像这样: @rem = ' PERL for Windows NT -
为什么 System.Windows.Clipboard(PresentationCore.dll) 对System.Windows.Thickness (PresentationFramework.
我想用来自 NUnit 测试的文本填充 Forms.Clipboard。 我遇到的第一个问题是剪贴板必须在STA模式下使用。我找到了 the solution (NUnit 2.5.x+) 在方法上设
当我想在我的应用程序中共享基本纯文本时,将其复制到剪贴板的选项不会显示在选择器列表中。我的代码有问题吗?还是我的设备设置有误? String code = getXMLCode(); Intent s
我在LibGDX开发游戏,游戏中有登录界面和注册界面。 HTML 版本的游戏有剪贴板的沙盒环境,意味着: 任何从游戏中复制的东西,都不能粘贴到游戏外&从外部复制的任何内容都不能粘贴到游戏的文本字段中
我有一个奇怪的问题,我相信我可能只需要一些权利来声明使其工作。 我有一些用户可以复制文本的 TextView ,并且可以将其粘贴到应用程序内的另一个文本字段中。但是当用户退出(或暂停)应用程序时,用户
我有一个小程序正在监听图像的剪贴板( Hook )。如果有存储或通过ctrl+c等复制的图像,我的程序会自动将图像粘贴到打开的word文档中。 代码: if (Clipboard.ContainsIm
我正在使用在 Linux Mint 上运行的终端仿真器(准确地说是 MATE),它在 Windows 托管的虚拟机中运行。我通过 ssh 连接到 CentOS Linux 上的 bash shell。
我发现自己在运行脚本并将这些运行的输出复制粘贴到电子邮件或其他一些文档中。有没有办法让复制到剪贴板的步骤成为脚本本身的一部分?我的大部分脚本都是 Perl 或 bat 文件,我在 Windows 上工
如何使用 .NET 框架访问剪贴板内容? 最佳答案 检查 Clipboard类及其 SetText\GetText 方法。 另请参阅本教程: Clipboard Copy and Paste with
我有一些代码要复制和粘贴: void WinClipboard::copy( const std::string& input ) { LPWSTR lptstrCopy;
我想获取当前存储在 Windows 剪贴板中的数据并将其保存在一个变量中,然后将数据放回剪贴板。 现在我正在使用这段代码: object l_oClipBrdData = Clipboard.GetD
引用topic这解释了如何将数据复制到 android 剪贴板,是否可以将视频/音频文件复制到剪贴板。 我假设视频/音频文件以二进制值存储并再次绑定(bind)以将它们作为视频/音频播放。 需要您的建
我是一名优秀的程序员,十分优秀!