Javascript到jQuery的转换代码
Javascript to jQuery convert code
我很难将以下代码更改为使用jQuery。有人能给我指正确的方向吗?
function addListItem() {
var listItem, container, dataList = document.getElementById('dataList');
// Create our list item
listItem = document.createElement('div');
listItem.setAttribute('data-bb-type', 'item');
listItem.setAttribute('data-bb-img', 'images/icons/icon11.png');
listItem.setAttribute('data-bb-title', 'Title ');
listItem.innerHTML = 'My description';
// Create a dummy container
container = document.createElement('div');
container.appendChild(listItem);
// Apply the styling
bb.imageList.apply([container]);
// Append the item
dataList.appendChild(container.firstChild);
// re-compute the scrolling area
if (bb.scroller) {
bb.scroller.refresh();
}
}
首先,让我告诉您,您的代码看起来非常好。
现在,要使用jQuery按ID选择元素,请使用ID选择器("#ID")。因此,不使用document.getElementById("dataList")
,而是使用:
$("#dataList")
要动态创建DOM元素,请使用jQuery( html, props )
。例如,要创建列表项,您可以使用:
listItem = $("<div />", {
data-bb-type: 'item',
data-bb-img: 'images/icon/icon11.png',
data-bb-title: 'Title'
}).html("My description");
最后,要将元素附加到另一个元素,请使用.append()
。使用此功能,可以使用以下方法创建虚拟元素:
container = $("<div />").append(listItem);
相关文章:
- JavaScript代码问题:我正在将对象转换为数组
- 如何将字母转换为二进制代码
- js代码从jQuery转换为原生代码
- 将javascript代码转换为jquery代码时出错
- Javascript到jQuery的转换代码
- 将代码转换为可重用对象
- 如何在html中将字符串转换为数字?以及如何将变量传递到scriptlet代码中
- 需要帮助编写在不使用toString的情况下将十进制转换为二进制的代码
- 将mongoose javascript代码转换为typescript.此引用丢失
- 应用将图表转换为图像的jqplot代码时Chrome挂起
- 将JQuery代码转换为等效的JavaScript代码
- 将Google AdWords转换跟踪(javascript)代码添加到PrestaShop/Smarty页面中
- Google Adwords转换代码集成到Prestashop中的CMS页面
- 如何在点击链接后添加Facebook转换代码
- Volusion Java Script -如何安装ROI跟踪转换代码
- 如何在javascript字符串转换代码
- Javascript:转换代码字符串和返回
- 有人能解释一下这个基本转换代码吗?
- 为什么以这种方式转换代码
- 理解简单的轮播转换代码