将以下 jQuery 代码转换为 YUI 2.x 代码

发布于 2024-07-18 18:30:00 字数 803 浏览 8 评论 0原文

免责声明:我根本没有使用 YUI 的经验。


我想知道以下几行 jQuery 代码在用 YUI 编写时会是什么样子。 另外,由于 YUI 使用分层依赖系统,因此需要包含哪些 .js 文件才能使代码正常工作?

1. 给定 HTML 元素的 ID,在该元素上应用多个样式规则。

$('#foo').css({ color: 'yellow', background: 'black' });

2. 链接:给定 HTML 元素的 ID,对其应用样式规则,向其添加 bar 类,并将其内容设置为“!”。

$('#foo').css('color', 'red').addClass('bar').html('!');

3. 将 LI 元素附加到 #menu

$('#menu').append('<li>An extra item</li>');

4. 基本事件绑定:每当单击 LI 元素时显示警报。

$('li').click(function() {
 alert('Clickety-click!');
});

Disclaimer: I have no experience using YUI at all.


I was wondering how the following lines of jQuery code would look when written in YUI. Also, since YUI is using a hierarchical dependency system, which .js files need to be included in order for the code to work?

1. Given the ID of an HTML element, apply multiple style rules on the element.

$('#foo').css({ color: 'yellow', background: 'black' });

2. Chaining: given an ID of an HTML element, apply a style rule on it, add a class of bar to it, and set its contents to '!'.

$('#foo').css('color', 'red').addClass('bar').html('!');

3. Append a LI element to #menu.

$('#menu').append('<li>An extra item</li>');

4. Basic event binding: show an alert whenever a LI element is clicked on.

$('li').click(function() {
 alert('Clickety-click!');
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失而复得 2024-07-25 18:30:00

免责声明:我不是 YUI 专家,可能有更好的方法来做这些事情。

另外,jQuery 很擅长完成您所提出的任务。 YUI 的构建更多是为了它的小部件,因此您可能会发现它的核心功能比 jQuery 稍微不那么全面(即,它不会取代您想要通过函数调用执行 DOM 的所有功能)。 我觉得人们使用 YUI 2.x 是因为他们想要小部件(我经常使用他们的菜单)。

#1:您需要包含以下内容:

<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/dom/dom-min.js"></script>

代码将是:

var el = YAHOO.util.Dom.get('foo');
YAHOO.util.Dom.setStyle(el, 'color', 'yellow');
YAHOO.util.Dom.setStyle(el, 'background', 'black');

或者...

YAHOO.util.Dom.setStyle('foo', 'color', 'yellow');
YAHOO.util.Dom.setStyle('foo', 'background', 'black');

#2:YUI 2.x 中没有链接,因此您的解决方案不会几乎相同:

var el = YAHOO.util.Dom.get('foo');
YAHOO.util.Dom.setStyle(el, 'color', 'red');
YAHOO.util.Dom.addClass(el, 'bar');
// Not sure how to set contents with YUI...  would just use:
el.innerHTML = "!";

无论如何,我不关心链接,我认为这段代码更具可读性。 (抱歉进行编辑。)

#3:同样,除了使用innerHTML之外,不知道如何直接设置html...我认为它只是:

var el = YAHOO.util.Dom.get('menu');
el.innerHTML += '<li>An extra item</li>';

#4:您必须为此解决方案包含不同的类:

<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-min.js" ></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/event/event-min.js" ></script>

代码如下:

var lis = document.getElementsByTagName("li");
YAHOO.util.Event.addListener(lis, 'click', function() {
        alert('Clickety-click!');
    });

再次抱歉,这些可能不是倒数第二个 YUI 解决方案。 另外,如果您担心不断使用“YAHOO.util.longname.method”,您可以轻松地创建自己的局部变量。 YUI 在他们的库中一直这样做:

(function() {
    var Event = YAHOO.util.Event;

    // Insert all your code here...
})();

Disclaimer: I'm not a YUI expert, there may be better ways to do these things.

Also, jQuery is good at doing what you've put up. YUI is built more for its widgets, so you may find its core functionality a little less comprehensive than jQuery's (i.e., it doesn't replace EVERYTHING you would ever want to do the DOM with function calls). I feel like one uses YUI 2.x because they want widgets (I use their menus quite a bit).

#1: You would need to include this:

<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/dom/dom-min.js"></script>

Code would be:

var el = YAHOO.util.Dom.get('foo');
YAHOO.util.Dom.setStyle(el, 'color', 'yellow');
YAHOO.util.Dom.setStyle(el, 'background', 'black');

Alternatively...

YAHOO.util.Dom.setStyle('foo', 'color', 'yellow');
YAHOO.util.Dom.setStyle('foo', 'background', 'black');

#2: There is no chaining in YUI 2.x, so your solution won't be nearly the same:

var el = YAHOO.util.Dom.get('foo');
YAHOO.util.Dom.setStyle(el, 'color', 'red');
YAHOO.util.Dom.addClass(el, 'bar');
// Not sure how to set contents with YUI...  would just use:
el.innerHTML = "!";

I don't care for chaining anyways, I think this code is much more readable. (Sorry for the editorializing.)

#3: Again, not sure how to set html directly besides just using innerHTML... I would think it'd just be:

var el = YAHOO.util.Dom.get('menu');
el.innerHTML += '<li>An extra item</li>';

#4: You'll have to include different classes for this solution:

<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-min.js" ></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/event/event-min.js" ></script>

And here's the code:

var lis = document.getElementsByTagName("li");
YAHOO.util.Event.addListener(lis, 'click', function() {
        alert('Clickety-click!');
    });

Again, sorry that these may not be the penultimate YUI solutions. Also, if you're worried about the constant usage of "YAHOO.util.longname.method", you can easily just make your own local variable. YUI does this all the time in their libraries:

(function() {
    var Event = YAHOO.util.Event;

    // Insert all your code here...
})();
三岁铭 2024-07-25 18:30:00

正如您从 Daniel 的回答中看到的那样,在 YUI 2 中编写代码非常冗长。该领域的大量工作已在 即将推出的 YUI 3。 下面是在 YUI 3 中编写代码的方式:

YUI().use('node', function(Y) {
    // #1
    Y.get('#foo').setStyles({ color: 'yellow', background: 'black' });

    // #2
    Y.get('#foo').setStyle('color', 'red').addClass('bar').set('innerHTML', '!');

    // #3
    Y.get('#menu').appendChild(Y.Node.create('<li>An extra item</li>'));

    // #4
    Y.all('li').on('click', function() {
        alert('Clickety-click!');
    });
}

As you can see from Daniel's answer it's quite verbose to write your code in YUI 2. A lot of work in this area has been done in the upcoming YUI 3. Here's how your code would be written in YUI 3:

YUI().use('node', function(Y) {
    // #1
    Y.get('#foo').setStyles({ color: 'yellow', background: 'black' });

    // #2
    Y.get('#foo').setStyle('color', 'red').addClass('bar').set('innerHTML', '!');

    // #3
    Y.get('#menu').appendChild(Y.Node.create('<li>An extra item</li>'));

    // #4
    Y.all('li').on('click', function() {
        alert('Clickety-click!');
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文