将params提供给通过document.createElement创建的div

时间:2017-03-16 07:01:23

标签: javascript html node.js

我想使用for loop创建多个div,我这样做

for(let i = 0; i < data.length; i++) {
  let divs = document.createElement('div');
  divs.className = 'button';
  divs.data-id = (data[i]).id;                  // error
  document.body.appendChild(divs);
}

现在我的问题是data-id是必需的,但会弹出错误。那么如何动态定义data-id

我也尝试将整个div作为另一个div的innerHTML,然后它不会渲染。那怎么能解决呢?

4 个答案:

答案 0 :(得分:2)

改为使用setAttribute()

public function check($id){
$dt = "hello";
return view('running' , compact('dt'));
}

另请注意,Javascript标识符中不允许使用连字符,它们将被解析为减法。

答案 1 :(得分:2)

您正在寻找divs.setAttribute('data-id', data[i].id);

如果您尝试阅读带有时髦字符的属性(例如示例中的破折号),则可以使用括号而不是点符号来访问它,并引用属性,如下所示:divs.attributes['data-id']

答案 2 :(得分:1)

for(let i = 0; i < data.length; i++) {
  let divs = document.createElement('div');
  var attr = document.createAttribute('data-id');
  attr.value = (data[i]).id;
  divs.setAttributeNode(attr);
  document.body.appendChild(divs);
}

答案 3 :(得分:1)

这应该有效:

var newDiv= $('<div/>', { id: 'foo', class: 'tclose'})

或使用Jquery替代品:

  

#include <iostream> using namespace std; class LinkedList { private: struct ListNode { int value; struct ListNode *next; }; ListNode *head; public: LinkedList() { head = NULL; } ~LinkedList(); const LinkedList& operator=(const LinkedList &rhs); void appendNode(int); void insertNode(int); void deleteNode(int); ListNode *removeLast(); }; void LinkedList::appendNode(int num) { ListNode *newNode; ListNode *nodePtr; newNode = new ListNode; newNode->value = num; newNode->next = NULL; if (!head) { head = newNode; } else { nodePtr = head; while (nodePtr->next) { nodePtr = nodePtr->next; } nodePtr->next = newNode; } } void LinkedList::insertNode(int num) { ListNode *newNode; ListNode *nodePtr; ListNode *preNode; newNode = new ListNode; newNode->value = num; if (!head) { head = newNode; newNode->next = NULL; } else { nodePtr = head; preNode = NULL; while (nodePtr->next) { preNode = nodePtr; nodePtr = nodePtr->next; } if (!preNode) // if new node is to be the 1st in the list { head = newNode; newNode->next = nodePtr; } else { preNode->next = newNode; newNode->next = nodePtr; } } } void LinkedList::deleteNode(int num) { ListNode *nodePtr; ListNode *preNode; if (!head) return; if (head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } else { nodePtr = head; while (nodePtr != NULL && nodePtr->value != num) { preNode = nodePtr; nodePtr = nodePtr->next; } if (nodePtr) { preNode->next = nodePtr->next; delete nodePtr; } } } const LinkedList& LinkedList::operator=(const LinkedList &rhs) { if (this != &rhs) { ListNode *curr, *nextNode; curr = head; nextNode = NULL; while (curr) { nextNode = curr->next; delete curr; curr = nextNode; } ListNode *cptr = new ListNode; cptr = rhs.head; cptr->next = NULL; temp = rhs.head->next; while (temp) { cptr->next = new ListNode; cptr->next = temp; cptr = cptr->next; cptr->next = NULL; temp = rhs.head->next; } } return *this; } LinkedList::ListNode* LinkedList::removeLast() { ListNode *nodePtr, *preNode; if (!head) { return; } nodePtr = head; preNode = NULL; while (nodePtr->next) { preNode = nodePtr; nodePtr = nodePtr->next; } preNode->next = NULL; return nodePtr; } LinkedList::~LinkedList() { ListNode *nodePtr; ListNode *nextNode; nodePtr = head; while (nodePtr) { nextNode = nodePtr->next; delete nodePtr; nodePtr = nextNode; } }