jQuery选择器比较表达式

时间:2014-05-02 16:44:31

标签: jquery

我从网站上复制了这段代码,它实现了我的需求,但有人可以翻译语法吗?

非常感谢你的帮助。

log.val(log.val() + ($('#defaultEntry').val() || 'blank') + '\n');
<script>
    $(document).ready(function () {
        $("#checkEmpty").click(function () {
            var log = $('#log');
            log.val(log.val() + ($('#defaultEntry').val() || 'blank') + '\n');
        })
    });
</script>

<p>
    <input id="defaultEntry" type="text" /></p>
<p>
    <input id="log" type="text" /></p>

3 个答案:

答案 0 :(得分:0)

我把它翻译成蒙古语。希望它有所帮助

  log.val (log.val () + ($ ('# defaultEntry') цэнийг () | | "хоосон") + \ 'N'.);
    <script>
         $ (бичиг баримт). бэлэн (функц () {
             $ ("# checkEmpty"). дарж (функц () {
                 VAR бүртгэлийн = $ ('# бүртгэл');
                 log.val (log.val () + ($ ('# defaultEntry') цэнийг () | | "хоосон") + \ 'N'.);
             })
         });
    </ скрипт>

    <p>
         үзсэн <Input id="defaultEntry" type="text" /> </ P>
    <p>
         үзсэн <Input id="log" type="text" /> </ P>

答案 1 :(得分:0)

我从你的问题中猜测这是让你感到困惑的一句话:

log.val(log.val() + ($('#defaultEntry').val() || 'blank') + '\n');

如果是这种情况,基本上这里发生的事情是你传递log.val()函数一个字符串参数,该参数由对log.val()连接(+)的调用组成值$('#defaultEntry') OR 'blank'加上换行符\n

特别是,这是要注意的部分:

$('#defaultEntry').val() || 'blank'

基本上这就是说||左边的表达式的一边是 falsy (即falsenull,{{1 }},0等),然后评估""右侧的表达式,在本例中为||字符串。这样做的结果是,如果'blank'为空,为空等,则您的函数将使用字符串log.val()

答案 2 :(得分:0)

<script>
    $(document).ready(function () { // execute the following after the HTML has loaded and the DOM is ready
        $("#checkEmpty").click(function () { // execute the following when the element with id="checkEmpty" is clicked
            var log = $('#log'); // assign the element with id="log" to a variable named 'log'
            log.val(log.val() + ($('#defaultEntry').val() || 'blank') + '\n');
            // take the concatenation of the current value of the element with id="log" (referenced by the 'log' variable)
            // with the current value of the element with id="defaultEntry" if not empty OR 'blank' (if the value of that element was empty)
            // and make that the new value of the element with id="log"
        });
    });
</script>