jQuery 可以通过选择器创建元素吗?

发布于 2024-09-14 23:44:00 字数 247 浏览 4 评论 0原文

通常我会在 jQuery 中创建一个像这样的元素 $('

')

我可以创建一个给定选择器的元素吗使用类似 $.create("div#errors.red") 的东西?哪里会返回一个代表 div 的 jQuery 对象,其 id 为 errors ,类为 red

Normally I would create an element in jQuery like this $('<div id="errors" class="red"></div>')

Can I create an element given a selector using something like $.create("div#errors.red")? Where that would return a jQuery object representing a div with the id of errors and the class of red?

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

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

发布评论

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

评论(4

笔芯 2024-09-21 23:44:00

你的意思是像 DOM 创建那样的 Zen Coding。您可以在 zen 编码 Google 项目页面上找到 zen 编码的语法。

用法如下:

1. 下载并添加 zen-0.1.a.js 文件 并将其添加到您的主页

2. 将此函数添加到您的项目

var zen = function(input) {
  return jQuery(zen_coding.expandAbbreviation(input, 'html', 'xhtml'));
};

3. 使用以下命令创建 jQuery dom:

var dom = zen('div#id.class');
$('body').append(dom);

What you mean is Zen Coding like DOM creation. You can find the syntax of zen coding on the zen coding google project page.

Usage would be:

1. download and add the zen-0.1.a.js file and add it to your homepage

2. add this function to your project

var zen = function(input) {
  return jQuery(zen_coding.expandAbbreviation(input, 'html', 'xhtml'));
};

3. create jQuery dom with:

var dom = zen('div#id.class');
$('body').append(dom);
夏末 2024-09-21 23:44:00

下面的插件

是我编写的一个插件,它允许您创建一个元素,只需用属性装饰它,然后将其附加到 DOM。以下是使用 .adorn() 实现您想要执行的操作的示例:

$("<div>").adorn("#errors",".red",">body");

上面创建了一个 div,添加了 ID 和 < code>Class 并将其全部附加到 body 中。请注意,您可以按任意顺序添加 appendTo 语法,因此这也有效:

$("<div>").adorn(">body","#errors",".red");

使用装饰创建的简单示例页面。

与使用一个连续字符串相反,我发现将每个类、id、值等作为参数传递更容易,因为这很明显描绘出它们。

语法:

  • .blah - 添加类 blah
  • #blah - 将 id 设置为 blah
  • < code>>blah - 将其附加到 blah
  • h:blah - 将innerHTML 设置为 blah
  • v:blah< /code> - 将值设置为 blah
  • 目前有 9 个选项
  • ...您可以轻松添加新功能。

请注意,使用时,冒号可以是任何单个字符,它不必是冒号,因此 h-blah 也可以。

其他用途:

这个插件的好处是它不仅可以用来装饰新创建的元素,还可以用来装饰现有的元素。例如,添加 3 个类并为页面上的所有 div 设置 HTML:

$("div").adorn(".one",".two",".three","h:blah");

jsFiddle 示例< /strong>

最后,如果你不小心传入了错误的标签。 向元素添加错误类以简化调试,但事情不会休息。

它是如何工作的:

这个插件的核心是利用自动可用的参数数组来保存所有传入的选项。使用 switch 语句子字符串方法

插件:

(function( $ ){
    jQuery.fn.adorn = function () {
          // Get the arguments array for use in the closure
        var $arguments = arguments;
          // Maintain chainability
        return this.each(function () {
            var $this = $(this);
            $.each($arguments, function(index, value) {
                  // These are just the options that quickly came
                  //   to mind, you can easily add more.
                  // Which option is used depends on the first
                  //   character of the argument passed in. The 2nd
                  //   char is sometimes used and sometimes ignored.
                switch (value.substring(0,1)) {
                    case "#":
                        $this.attr("id",value.substring(1));
                    break;
                    case ".":
                        $this.addClass(value.substring(1));
                    break;
                    case ">":
                        $this.appendTo(value.substring(1));
                    break;                          
                    case "a":
                        $this.attr("alt", value.substring(2));
                    break;
                    case "h":
                        $this.html(value.substring(2));
                    break;
                    case "i":
                        $this.attr("title", value.substring(2));
                    break;
                    case "s":
                        $this.attr("src", value.substring(2));
                    break;
                    case "t":
                        $this.attr("type", value.substring(2));
                    break;
                    case "v":
                        $this.attr("value", value.substring(2));
                    break;
                    default:
                          // if the wrong key was entered, create
                          // an error class.
                        $this.addClass("improper-adorn-label");
                }
            });
        });
    };
})( jQuery );

A Plugin

Below is a plugin I wrote that allows you to create an element, simply adorn it with attributes, and append it to the DOM. Here's an example use of .adorn() with what you wanted to do:

$("<div>").adorn("#errors",".red",">body");

The above creates a div, slaps on an ID and a Class and appends it all to the body. Note that you can add the appendTo syntax in any order, so this will work too:

$("<div>").adorn(">body","#errors",".red");

Simple example page created with adorn.

As opposed to using one continuos string, I found it easier to pass each class, id, value, etc. in as an argument, since that clearly delineates them.

Syntax:

  • .blah - adds class blah
  • #blah - sets id to blah
  • >blah - appends this to blah
  • h:blah - sets innerHTML to blah
  • v:blah - sets value to blah
  • There's 9 options currently
  • ... you can easily add new functionality.

Note that when used, the colon, can be any single character, it doesn't have to be a colon, so h-blah would also work.

Other Uses:

The nice thing about this plugin is that it cannot only be used to adorn newly created elements, but it can also be used to adorn existing elements. For example to add 3 classes and set the HTML for all divs on a page:

$("div").adorn(".one",".two",".three","h:blah");

jsFiddle example

Finally, if you accidentally pass in the wrong label. An error class is added to the element to ease debugging, but things will not break.

How it Works:

The heart of this plugin is making use of the automatically available arguments array to hold all the passed in options. The options are parsed using a switch statement and the substring method.

The plugin:

(function( $ ){
    jQuery.fn.adorn = function () {
          // Get the arguments array for use in the closure
        var $arguments = arguments;
          // Maintain chainability
        return this.each(function () {
            var $this = $(this);
            $.each($arguments, function(index, value) {
                  // These are just the options that quickly came
                  //   to mind, you can easily add more.
                  // Which option is used depends on the first
                  //   character of the argument passed in. The 2nd
                  //   char is sometimes used and sometimes ignored.
                switch (value.substring(0,1)) {
                    case "#":
                        $this.attr("id",value.substring(1));
                    break;
                    case ".":
                        $this.addClass(value.substring(1));
                    break;
                    case ">":
                        $this.appendTo(value.substring(1));
                    break;                          
                    case "a":
                        $this.attr("alt", value.substring(2));
                    break;
                    case "h":
                        $this.html(value.substring(2));
                    break;
                    case "i":
                        $this.attr("title", value.substring(2));
                    break;
                    case "s":
                        $this.attr("src", value.substring(2));
                    break;
                    case "t":
                        $this.attr("type", value.substring(2));
                    break;
                    case "v":
                        $this.attr("value", value.substring(2));
                    break;
                    default:
                          // if the wrong key was entered, create
                          // an error class.
                        $this.addClass("improper-adorn-label");
                }
            });
        });
    };
})( jQuery );
薔薇婲 2024-09-21 23:44:00

我不认为你可以做到这一点,我知道的更接近的事情是你可以

var newdiv = $.DIV({ className: "red", id: "errors"});

创建 div 并且你可以

使用这个插件 http://mg.to/2006/02/27/easy-dom-creation-for-jquery-和原型

I dont think you can do that, the closer thing i know is that you can do

var newdiv = $.DIV({ className: "red", id: "errors"});

the div is created and you can append events and stuff directly to the variable

using this plugin http://mg.to/2006/02/27/easy-dom-creation-for-jquery-and-prototype

滥情空心 2024-09-21 23:44:00

你可以尝试这个方法:

$("<div id="errors" class="red"></div>").appendTo("body");

You can try this method:

$("<div id="errors" class="red"></div>").appendTo("body");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文