开发者

jQuery like Linq?

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 (r开发者_高级运维egex, 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.


You 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 compare, 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 using select()
  • $(document).ready(callback) syntax is kind of deprecated, use jQuery(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;
            });

         }

    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜