English 中文(简体)
jQuery像Linq一样?
原标题:jQuery like Linq?

我的谷歌搜索技术让我很失望。

我的理解是,有一种方法可以让我以一种与Linq非常相似的方式开始编写jQuery。我不一定是指语法,而是指定义(使用内联/匿名函数)。然而,我不明白该怎么做。

因此,为了帮助我,我创建了一个带有TextArea的页面,当我在其中插入换行符时,我希望TextArea的高度增加(即rows=10或其他)。因此,我想使用以下脚本来完成此操作:

<script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $(#mytxt).keypress(function () {
                $(this).attr(rows, $(this).val().select(function (x) { x ==  
 }).count());
            });
        });
</script>

那么,如果我继续以林克式的方式这样做,我该怎么做呢?请注意,我知道我可以在javascript中以许多其他方式(regex、在分隔符上拆分字符串等)来实现这一点,但我希望以类似Linq的方式来实现。此外,我不想使用任何第三方库/插件来帮助解决这个问题-我正在寻找jQuery中标准的东西。同样,确切的语法并不是我想成为的Linq(尽管我是这样写示例的)-只是概念。

最佳回答

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

因此,您可以选择以下内容:

<script language="javascript" type="text/javascript">
    $(function () {
        $("#mytxt").keypress(function (event) {
            if(event.which==13)
                $(this).attr("rows", $(this).val().split("
").length );
        });
    });
</script>

请注意,val()返回一个标量(一个通用的JavaScript字符串),从现在起与jQuery无关。split()是一个用于字符串操作的JavaScript函数,length也是一个用于测量数组长度的JavaScript属性。

问题回答

试试这个:

$(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( 
 ).length;
            });

         }

    });
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签