admin管理员组文章数量:1390718
My googling skills are failing me.
My understanding is that there is a way that I can start writing jQuery in a way that makes it very similar to Linq. I don't necessarily mean in syntax but more in definition (using inline/anonymous functions). However, I'm failing to understand how to take it there.
So to help me, I've created a page with a TextArea and I want the TextArea to increase in height (i.e. rows=10
or whatever) as I insert newlines in them. So I'd like to do this with the following script:
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$(#mytxt).keypress(function () {
$(this).attr(rows, $(this).val().select(function (x) { x == '\n'}).count());
});
});
</script>
So how can I do this where I continue to do this in a Linq-esque way? Note that I know I can do this in many other ways within javascript (regex, splitting a string on a delimiter, etc.) but I want to do this in a Linq-like way. Also, I don't want to use any third-party libraries/plugins to help with this - I'm looking for something that is standard in jQuery. Again, exact syntax is not what I want to be like Linq (even though that's kinda how I wrote my example) - just the concept.
My googling skills are failing me.
My understanding is that there is a way that I can start writing jQuery in a way that makes it very similar to Linq. I don't necessarily mean in syntax but more in definition (using inline/anonymous functions). However, I'm failing to understand how to take it there.
So to help me, I've created a page with a TextArea and I want the TextArea to increase in height (i.e. rows=10
or whatever) as I insert newlines in them. So I'd like to do this with the following script:
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$(#mytxt).keypress(function () {
$(this).attr(rows, $(this).val().select(function (x) { x == '\n'}).count());
});
});
</script>
So how can I do this where I continue to do this in a Linq-esque way? Note that I know I can do this in many other ways within javascript (regex, splitting a string on a delimiter, etc.) but I want to do this in a Linq-like way. Also, I don't want to use any third-party libraries/plugins to help with this - I'm looking for something that is standard in jQuery. Again, exact syntax is not what I want to be like Linq (even though that's kinda how I wrote my example) - just the concept.
Share asked Feb 10, 2011 at 20:43 JaxidianJaxidian 13.5k9 gold badges88 silver badges131 bronze badges 3- 1 Not entirely sure what you are asking for. Those functions are about as anonymous as possible using javascript. – Hux Commented Feb 10, 2011 at 20:48
-
I want to figure out what to replace
$(this).val().select(function (x) { x == '\n'}).count()
with to make this work. That jQuery code does not work but I believe it's close to working to count the number of newLines in themytxt
textarea's text. – Jaxidian Commented Feb 10, 2011 at 20:50 - I think your thinking is in the lamdas space that both lanaguages use – mozillanerd Commented Feb 10, 2011 at 21:25
2 Answers
Reset to default 5You are somehow correct. LINQ operates with IQueryable type, while jQuery operates with a jQuery :). Though IQueryable is much more abstract than jQuery object, in terms of data. You misunderstood, however, the concept of what data is there which is non-jQuery. The jQuery object revolves around DOM objects, and only operates with them. Values may be passed and tossed around, but there has to be DOM, or jQuery is pointless. To pare, IQueryable was made to work with data, with values, or with objects. These are 2 totally different concepts, which only have some similar principle when working with them.
$(#mytxt)
should be$("#mytxt")
val()
returns a scalar, which is not jQuery, so you can't apply an event handler usingselect()
$(document).ready(callback)
syntax is kind of deprecated, usejQuery(callback)
instead, or just$(callback)
- I would advise you check the event, to see what key was pressed, so as not to resize the DOM element each time a key is pressed. You can pass an argument, i.e.
event
to the callback function, which will receive data about the event.event.which
would be the character code. For more info, visit this page
as a result, you would have something like this as an option:
<script language="javascript" type="text/javascript">
$(function () {
$("#mytxt").keypress(function (event) {
if(event.which==13)
$(this).attr("rows", $(this).val().split("\n").length );
});
});
</script>
Note that val()
returns a scalar (a generic JavaScript string), which has nothing to do with jQuery from here on. split()
is a JavaScript function for string manipulation, so is length
a JavaScript attribute for measuring array length.
Try this:
$(function() {
$('#mytxt').keypress(function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Only do this when enter is pressed
$(this).attr('rows', function() {
var val = $(this).val();
return val.split('\n').length;
});
}
});
});
本文标签: javascriptjQuery like LinqStack Overflow
版权声明:本文标题:javascript - jQuery like Linq? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744650570a2617658.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论