admin管理员组文章数量:1357276
I need to change the width of a td using javascript. The value of the width will be pulled in and then I need to multiple it to get a percentage. I just need to know how to write the js so far I have got to here but it is not working
<script language="JavaScript">
var divWidth = 66*0.4;
document.write("<td width="+divWidth+">");
document.getElementById("myRow").write("<td width="+divWidth+">");
</script>
<table>
<tr>
<td id="myRow"></td>
</tr>
I need to change the width of a td using javascript. The value of the width will be pulled in and then I need to multiple it to get a percentage. I just need to know how to write the js so far I have got to here but it is not working
<script language="JavaScript">
var divWidth = 66*0.4;
document.write("<td width="+divWidth+">");
document.getElementById("myRow").write("<td width="+divWidth+">");
</script>
<table>
<tr>
<td id="myRow"></td>
</tr>
Share
Improve this question
asked Sep 6, 2010 at 17:13
kristinakristina
611 gold badge1 silver badge2 bronze badges
2 Answers
Reset to default 3I think you want this:
<script type="text/javascript">
var divWidth = 66*0.4;
document.getElementById("myRow").setAttribute('width', divWidth);//defaults to pixels
//or if you really want this as a percentage
document.getElementById("myRow").setAttribute('width', divWidth+'%');//percent
</script>
<table>
<tr>
<td id="myRow"></td>
</tr>
</table>
However, there are some things I would adjust in your code. I changed the language attribute in the script tag (now deprecated) to a type="javascript"
. The id
on your TD element is myRow
I would change this to something reflecting the cell vs. the row.
Try using jQuery, it's a Javascript library/framework and what you're trying to do above will be ten times easier using jQuery.
With jQuery, your code will be:
$(document).ready(function () {
var newWidth = $("#myRow").width();
$("#myRow").width(newWidth*0.4);
}
//edit:
Sorry, I had the multiple wrong.
本文标签: javascriptdynamical change td width with jsStack Overflow
版权声明:本文标题:javascript - dynamical change td width with js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744074731a2586534.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论