【问题标题】:Add HTML elements to DOM with JQuery nicely使用 JQuery 很好地将 HTML 元素添加到 DOM
【发布时间】:2019-03-02 05:22:07
【问题描述】:

目前,我在 DOM 中添加元素,并在双引号内附加 html,这可能会变得非常混乱且难以阅读,尤其是如果其中有变量,在 React 中你会使用干净的 JSX ,有没有办法在普通的 JQuery 脚本或类似 JSX 的脚本中使用 JSX?

【问题讨论】:

  • 到目前为止您尝试过什么?请分享一下
  • @bhushan kawadkar,到目前为止,我似乎找不到解决方案,除了做一堆乱七八糟的 html 和变量。
  • @BhushanKawadkar 没有代码,并不是每个问题都不好。这实际上是一个非常有趣的话题。如果还没有的话,编写一个可以做到这一点的 jQuery 插件可能会很有趣。
  • 当您使用 API 端点获取帖子对象时,这个问题非常烦人,然后您需要在没有 JQuery 的情况下构建帖子,然后将其附加到帖子容器中。

标签: jquery html syntax append jsx


【解决方案1】:

是的,有两种选择:

  1. 模板文字

  2. JSX

模板文字

在现代 JavaScript 中,您可以使用模板文字而不是字符串文字,并且它们可以包含带有 JavaScript 表达式的占位符:

let counter = 0;

$(`<table class="main">
    <tbody>
        <tr>
            <td>Cell ${counter++}</td>
            <td>Cell ${counter++}</td>
        </tr>
        <tr>
            <td>Cell ${counter++}</td>
            <td>Cell ${counter++}</td>
        </tr>
    </tbody>
</table>`).appendTo(document.body);
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

肯定还有一些尴尬,但它比字符串文字要好得多。

更多关于模板字面量on MDN

JSX

JSX 不限于 React。它有它的own specification 和多个实现,例如jsx-transform

例如,这是一个简单的 Node.js 包装器,它使用它来转译文件:

var jsx = require('jsx-transform');

console.log(jsx.fromFile("input.jsx", {
  factory: 'be'
}));

如果input.jsx 是:

let counter = 0;
let table =
    <table class="main">
        <tbody>
            <tr>
                <td>Cell {counter++}</td>
                <td>Cell {counter++}</td>
            </tr>
            <tr>
                <td>Cell {counter++}</td>
                <td>Cell {counter++}</td>
            </tr>
        </tbody>
    </table>;
table.appendTo(document.body);

(请注意,这是 class="main",而不是 className="main"。使用 className 是 React 的事情,以避免出现自 2009 年 ES5 出现以来一直没有问题的问题。) em>

输出将是:

let counter = 0;
let table =
    be('table', {class: "main"}, [
        be('tbody', null, [
            be('tr', null, [
                be('td', null, ["Cell ", counter++]),
                be('td', null, ["Cell ", counter++])
            ]),
            be('tr', null, [
                be('td', null, ["Cell ", counter++]),
                be('td', null, ["Cell ", counter++])
            ])
        ])
    ]);
table.appendTo(document.body);

请注意 JSX 表达式是如何转换为对工厂函数的调用(在该示例中为 be;React 的工厂函数是 React.createElement)。您所要做的就是提供 be 函数并将转换器集成到您的构建链中(jsx-transform 预烘焙了插入 Browserify 的能力)。

您使用 jQuery 的 be 可能看起来像这样:

function be(tagName, attributes, children) {
    const result = $("<" + tagName + "/>");
    if (attributes) {
        result.attr(attributes);
    }
    if (children) {
        if (Array.isArray(children)) {
            for (const child of children) {
                result.append(child);
            }
        } else {
            result.append(child);
        }
    }
    return result;
}

使用转换结果的 be 函数的实时示例:

let counter = 0;
let table =
    be('table', {class: "main"}, [
        be('tbody', null, [
            be('tr', null, [
                be('td', null, ["Cell ", counter++]),
                be('td', null, ["Cell ", counter++])
            ]),
            be('tr', null, [
                be('td', null, ["Cell ", counter++]),
                be('td', null, ["Cell ", counter++])
            ])
        ])
    ]);
table.appendTo(document.body);

function be(tagName, attributes, children) {
    const result = $("<" + tagName + "/>");
    if (attributes) {
        result.attr(attributes);
    }
    if (children) {
        if (Array.isArray(children)) {
            for (const child of children) {
                result.append(child);
            }
        } else {
            result.append(child);
        }
    }
    return result;
}
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

令人惊讶的是,它真的就这么简单。

【讨论】:

  • 模板文字对我来说似乎是前进的方向,但 JSX 实现看起来很有趣。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 2015-09-23
  • 1970-01-01
  • 2010-09-28
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
相关资源
最近更新 更多