gpt4 book ai didi

javascript - 用于反转文本框值的 jquery 插件

转载 作者:行者123 更新时间:2023-12-03 11:33:19 25 4
gpt4 key购买 nike

我的目标是在单击按钮时反转文本框中的文本。到目前为止我所做的就是这样。我的 HTML

<html xmlns="http://www.w3.org/1999/xhtml">    
<head>
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.reverse.js"></script>
</head>
<body>
<p>mithun</p>
<input name="writehere" id="ch" type="text" class="jst" />
<input name="check" type="button" value="b1" id="b1"/>
<script type="text/javascript">
$("#b1").click(function() {
$('p').reverseText();
});</script>
</body></html>

我的 jquery.reverse.js 插件是

(function($) {

// jQuery plugin definition
$.fn.reverseText = function(params) {

// merge default and user parameters

params = $.extend( {minlength: 0, maxlength: 99999}, params);

// traverse all nodes
this.each(function() {

// express a single node as a jQuery object
var $t = $(this);

// find text
var origText = $t.text(), newText = '';

// text length within defined limits?
if (origText.length >= params.minlength && origText.length <= params.maxlength) {

// reverse text
for (var i = origText.length-1; i >= 0; i--) newText += origText.substr(i, 1);
$t.text(newText);

}

});

// allow jQuery chaining
return this;
};
})(jQuery);

如果我想反转 <p></p> 的文本,它就可以工作。 。但我想要做的是在按钮单击时反转文本框中写入的文本,并使用 id="ch"。该插件将如何实现此目的?任何人都可以给我任何建议如何编写该插件吗?

最佳答案

使用.val()而不是.text()Example

(function($) {

// jQuery plugin definition
$.fn.reverseText = function(params) {

// merge default and user parameters

params = $.extend( {minlength: 0, maxlength: 99999}, params);

// traverse all nodes
this.each(function() {

// express a single node as a jQuery object
var $t = $(this);

// find text
var origText = $t.val(), newText = '';

// text length within defined limits?
if (origText.length >= params.minlength && origText.length <= params.maxlength) {

// reverse text
for (var i = origText.length-1; i >= 0; i--) newText += origText.substr(i, 1);
$t.val(newText);

}

});

// allow jQuery chaining
return this;
};
})(jQuery);

关于javascript - 用于反转文本框值的 jquery 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26647326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com