javascript 如何在javascript中为td动态设置align属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5116466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:00:12  来源:igfitidea点击:

How to dynamically set align property for td in javascript?

javascriptjquery

提问by JAB

Here is my code, my question is in the comment:

这是我的代码,我的问题在评论中:

function (align) {    
    var column = $(`'<td>'`);  
  // now i need syntax to set align property to this td element  
  // column.align = align (not working)  
}

As shown, column.align = alignis not working.

如图,column.align = align不工作。

Where am I going wrong?

我哪里错了?

回答by krtek

it seems you're using jQuery, so you can do something like :

看来您正在使用 jQuery,因此您可以执行以下操作:

column.attr('align', 'right');

回答by Sukhjeevan

Try this one:

试试这个:

$(column).attr("align","left");

回答by arnemart

Looks like you're using jQuery? Use column.attr('align', 'whatev')or better yet, use CSS.

看起来您正在使用 jQuery?使用column.attr('align', 'whatev')或更好的是,使用 CSS。

回答by Aron Rotteveel

Judging from the $selector, you are using jQuery.

$选择器来看,您正在使用jQuery

In that case, columnis not a DOM element, but a jQuery selector result, and you have to treat it accordingly:

在这种情况下,column它不是一个 DOM 元素,而是一个jQuery 选择器 result,你必须相应地对待它:

$(column).attr('align', 'left');

回答by vishal_b

Try this, myTD.setAttribute("align", "right");

试试这个, myTD.setAttribute("align", "right");