JavaScript 创建带属性的元素

huangapple go评论78阅读模式
英文:

javascript createElement with property

问题

以下代码可以正常工作:

const element = document.createElement('div');
element.classList.add('person');

但是以下代码不起作用:

const element = document.createElement('div').classList.add('person');
英文:

I'm trying to add a element from Javascript with classname person.

It works perfectly fine by below code.

const element = document.createElement('div');
element.classList.add('person');

But why the below not working?

const element = document.createElement('div').classList.add('person');

答案1

得分: 1

document.createElement()返回一个元素,但在元素上调用.classList.add不会返回相同的元素。

在你的第一个代码中,element成为一个新创建的元素。然后在下一行,你调用它的.classList.add()方法给该元素添加一个类。element变量本身从未改变,但.classList.add()改变了element的某个内部属性。

在你的第二个代码中,你使用document.createElement()创建一个新元素,然后立即调用.classList.add()给该元素添加一个类。然后.classList.add()不返回任何内容,所以element变量的值是undefined。当将函数链接在一起时,变量只会被赋予函数返回的最后一个值。

如果你真的想让最后一个例子生效,那么你需要创建一个新的函数,调用.classList.add()然后返回该元素,像这样:

function addClass(el, className) {
  el.classList.add(className);
  return el;
}

const element = addClass(document.createElement('div'), 'person');
console.log(element);
英文:

document.createElement() returns an element, but calling .classList.add on an element doesn't return that same element.

In your first code, element becomes a newly created element. Then on the next line, you call its .classList.add() method to add a class to that element. The element variable itself was never changed, but .classList.add() changed some internal property of element.

In your second code, you create a new element with document.createElement(), then immediately call .classList.add() to add a class to that element. Then .classList.add() doesn't return anything, so the element variable has the value undefined. Variables are only assigned the last value returned by a function when chaining functions together.

If you really wanted to make the last one work, then you'd need to create a new function that calls .classList.add() then returns the element, like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function addClass(el, className) {
  el.classList.add(className);
  return el;
}

const element = addClass(document.createElement(&#39;div&#39;), &#39;person&#39;);
console.log(element);

<!-- end snippet -->

答案2

得分: 1

以下是要翻译的内容:

代码片段:

document.createElement('div').classList.add('person')

被认为无效,因为classList.add()方法不会返回添加了类的当前元素。而是返回undefined。

正确的方法是首先使用document.createElement('div')创建对象,然后将类添加到元素。

英文:

The code snippet:

document.createElement(&#39;div&#39;).classList.add(&#39;person&#39;)

is not considered valid because the classList.add() method does not return the current element with the added class. Instead, it returns undefined.

The correct approach is to first create the object using document.createElement(&#39;div&#39;), and then add the class to the element.

答案3

得分: 0

我知道实现这个功能的最简单方法是:

const create = (elementType, properties) => Object.assign(
  document.createElement(elementType),
  properties
);

document.querySelector('body').append(create('div', {classList: 'person'}));

关于实际问题:

let element = document.createElement('div');
element.classList.add('person');
document.querySelector('body').append(element);
英文:

The easiest way I'm aware of to write this functionality is:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// a named Arrow function, that takes two arguments:
// elementType: String, the type of element you wish to create (&#39;div&#39;, &#39;span&#39;, &#39;p&#39;...), and
// properties: Object detailing the properties, and their values, you wish to assign to
//             the created element, eg {...className: &#39;person&#39;, textContent: &#39;This is text&#39;...}
// Object.assign() takes multiple Objects and copies the enumerable properties of one or more
// Objects and to the first Object (the &#39;target&#39;), here this takes the properties from
// the properties Object, and copies them into the created element:
const create = (elementType, properties) =&gt; Object.assign(
  document.createElement(elementType),
  properties
);

// here we retrieve the &lt;body&gt; element, and then
document.querySelector(&#39;body&#39;)
  // call Element.append() to append the created-element (from
  // the create() function) to the document:
  .append(create(&#39;div&#39;, {classList: &#39;person&#39;}));

<!-- language: lang-css -->

body {
  padding: 1rem;
}

.person {
  border: 2px solid currentColor;
}

.person::before {
  content: &#39;This is an element with the class of &quot;person.&quot;&#39;;
}

div {
  outline: 4px solid #f90;
}

<!-- end snippet -->

As for the actual question:

> But why [is] the below not working?

const element = document.createElement(&#39;div&#39;).classList.add(&#39;person&#39;);

That's due to final use of the classList API, as the classList.add() method as it returns only undefined:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let element = document.createElement(&#39;div&#39;);

console.log(element.classList.add(&#39;person&#39;));

document.querySelector(&#39;body&#39;).append(element);

<!-- language: lang-css -->

body {
  padding: 1rem;
}

.person {
  border: 2px solid currentColor;
}

.person::before {
  content: &#39;This is an element with the class of &quot;person.&quot;&#39;;
}

div {
  outline: 4px solid #f90;
}

<!-- end snippet -->

References:

huangapple
  • 本文由 发表于 2023年5月20日 23:12:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定