Javascript ajax 语法创建 html 行

标签 javascript html ajax

我需要在 javascript 函数中使用 ajax 创建一行,但在找到正确的语法时遇到了很多麻烦。

这是我想要得到的 HTML 最终结果:

<td style="text-align:center">
<input type="image" src="nienteico.png" style="cursor:pointer; width:40px; height:40px" id="5.51" class="ajaxEdit" onclick="cambiastato(5.51)">
</td>

这是需要修改的JS代码:

<td style="+'text-align:center'+"><input type="+'image'+" src="+'nienteico.png'+" style="+'cursor:pointer; width:40px; height:40px'+" id="+'5.51'+" class="+'ajaxEdit'+" onclick="+'cambiastato(5.51)'+"></input></td>

最后这是我使用我之前编写的js获得的HTML:

<td style="text-align:center"><input type="image" src="nienteico.png" style="cursor:pointer;" width:40px;="" height:40px="" id="5.51" class="ajaxEdit" onclick="cambiastato(5.51)"></td>

提前致谢!

最佳答案

不要陷入肮脏的字符串连接,而是选择更干净的代码。

var row = $('<td />', {    style: "text-align:center"}).append($('<input />', {
    type: 'image',
    src: 'nienteico.png',
    'class': 'ajaxEdit'
}).css({
    id: '5.51',
    cursor: 'pointer',
    width: '40px',
    height: '40px',
}).click(function() {    cambiastato(5.51);    }));

关于Javascript ajax 语法创建 html 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27342671/

相关文章:

javascript - 执行顺序行为

javascript - jQuery:.width() 返回 css 设置的百分比,直到窗口调整大小,然后返回像素宽度。为什么?

javascript - Javascript 中的全局变量和局部变量

html - 我怎样才能把一个标签放在一个盒子的中间

html - 做一个 :after work in IE 9?

javascript - 使用 jquery 从头开始​​自动完成

javascript - 创建一个新的 JSON 数组,其中包含来自另一个 JSON 的特定属性

javascript - Firebase Storage Web - 创建引用不起作用

javascript - 加载页面内容而不刷新/

javascript - 禁用复选框上的输入框 jquery NO onchange