如何在代码中为对象添加宽度? var widthx=document.getElementById('testx'); var newwidth=document.getElementById('mycontent')。style.width=widthx.width+45//不工作 #testx { 宽度:950px; } #霉菌含量 { 宽度:800px;//此宽度需要从javascript更改为950+45 px } ....

如何在代码中为对象添加宽度? var widthx=document.getElementById('testx'); var newwidth=document.getElementById('mycontent')。style.width=widthx.width+45//不工作 #testx { 宽度:950px; } #霉菌含量 { 宽度:800px;//此宽度需要从javascript更改为950+45 px } ....,javascript,Javascript,我想将mycontent的宽度更改为testx width+45像素(px)。有人知道如何在javascript中实现这一点吗?我发现两个问题 首先,widthx.width不存在。您应该使用widthx.offsetWidth 其次,对于…style.width,简单的数字不是正确的值。例如,使用widthx.offsetWidth+45+'px'。我发现两个问题 <script language="javascript"> var widthx = document.getEle

我想将mycontent的宽度更改为testx width+45像素(px)。有人知道如何在javascript中实现这一点吗?

我发现两个问题

首先,
widthx.width
不存在。您应该使用
widthx.offsetWidth

其次,对于
…style.width
,简单的数字不是正确的值。例如,使用
widthx.offsetWidth+45+'px'

我发现两个问题

<script language="javascript">
var widthx = document.getElementById('testx');
var newwidth = document.getElementById('mycontent').style.width = widthx.width + 45 //not working
</script>

<style type="text/css">
#testx
{
width: 950px;
}

#mycontent
{
width: 800px; //this width needs to change from javascript to 950 + 45 px
}

....
首先,
widthx.width
不存在。您应该使用
widthx.offsetWidth

其次,对于
…style.width
,简单的数字不是正确的值。例如,使用
widthx.offsetWidth+45+'px'


<script language="javascript">
var widthx = document.getElementById('testx');
var newwidth = document.getElementById('mycontent').style.width = widthx.width + 45 //not working
</script>

<style type="text/css">
#testx
{
width: 950px;
}

#mycontent
{
width: 800px; //this width needs to change from javascript to 950 + 45 px
}

....
#testx { 宽度:950px; } #霉菌含量 { 宽度:800px;//此宽度需要从javascript更改为950+45 px } 函数onlyNumber(str){ str=str.toString(); 返回str.replace(/\D/g'); } var widthx=onlyNumber(document.getElementById('testx').style.width)*1; document.getElementById('mycontent').style.width=(widthx+45)+“px”;
使用jQuery:

<style type="text/css">
#testx
{
    width: 950px;
}

#mycontent
{
    width: 800px; //this width needs to change from javascript to 950 + 45 px
}
</style>

<!-- YOUR HTML HERE -->

<script language="javascript">
function onlyNumber(str) {
    str = str.toString();
    return str.replace(/\D/g, '');
}
var widthx = onlyNumber(document.getElementById('testx').style.width) * 1;
document.getElementById('mycontent').style.width = (widthx + 45) + "px";
</script>

函数onlyNumber(str){
str=str.toString();
返回str.replace(/\D/g');
}
$(文档).ready(函数(){
var widthx=onlyNumber($(“#testx”).css(“width”)*1;
$(“#mycontent”).css(“宽度”,“宽度x+45)+“px”);
});
#testx
{
宽度:950px;
}
#霉菌含量
{
宽度:800px;//此宽度需要从javascript更改为950+45 px
}

#testx
{
宽度:950px;
}
#霉菌含量
{
宽度:800px;//此宽度需要从javascript更改为950+45 px
}
函数onlyNumber(str){
str=str.toString();
返回str.replace(/\D/g');
}
var widthx=onlyNumber(document.getElementById('testx').style.width)*1;
document.getElementById('mycontent').style.width=(widthx+45)+“px”;
使用jQuery:

<style type="text/css">
#testx
{
    width: 950px;
}

#mycontent
{
    width: 800px; //this width needs to change from javascript to 950 + 45 px
}
</style>

<!-- YOUR HTML HERE -->

<script language="javascript">
function onlyNumber(str) {
    str = str.toString();
    return str.replace(/\D/g, '');
}
var widthx = onlyNumber(document.getElementById('testx').style.width) * 1;
document.getElementById('mycontent').style.width = (widthx + 45) + "px";
</script>

函数onlyNumber(str){
str=str.toString();
返回str.replace(/\D/g');
}
$(文档).ready(函数(){
var widthx=onlyNumber($(“#testx”).css(“width”)*1;
$(“#mycontent”).css(“宽度”,“宽度x+45)+“px”);
});
#testx
{
宽度:950px;
}
#霉菌含量
{
宽度:800px;//此宽度需要从javascript更改为950+45 px
}


尝试在
widthx
widthx.width
上执行
控制台。记录
警报
-这将帮助您发现问题。会发生什么情况
style.width
将是一个字符串,如果要执行数学运算,必须先将其转换为数字。有趣的是,我知道,但我不知道syntax@JarM,当您尝试
console.log
时发生了什么(您确实正确地尝试了,或者您不关心解决此问题?),关于您的问题,这说明了什么?尝试在
widthx
widthx.width
上执行
console.log
alert
-这将帮助您找到问题。会发生什么
style.width
将是一个字符串,如果要执行数学运算,必须先将其转换为数字。有趣的是,我知道,但我不知道syntax@JarM,当您尝试
console.log
时发生了什么(您确实正确地尝试了,或者您不关心解决此问题?),关于你的问题,这告诉了你什么?@JarM,最后一句话提供了一个直接的例子(尽管它保留了invalud
widthx.width
call)。你能把这三个单词写在:getElementById('mycontent')之后吗?style.width=@JarM:what是“把这三个单词写在:…”之后吗?糟糕,我的评论中有一些错误@JarM:我在这里根据您的代码创建了一个示例:如您所见,#由于JavaScript代码,mycontent更宽了。@JarM,最后一句提供了一个直接的示例(尽管它保留了invlud
widthx.width
call)。您能在以下三个词之后给我写下这三个词吗:getElementById('mycontent')。style.width=@JarM:What is is is is is“在……之后给我写下这三个字。。。“?我的错,我的评论中有一些错误@JarM:我在这里根据您的代码创建了一个示例:正如您所看到的,#由于JavaScript代码,mycontent的范围更广。您需要更改内容,请将JS放在底部。PS:使用jQuery更容易。现在就试试。我没有时间学习新的语言,我一整天都在尝试,你的例子不起作用,一个也不起作用。现在你告诉我,我应该去学习一个新的框架来解决这个简单的问题。没有thx。我建议你学习jQuery,你不会后悔的。我给你举了一个例子。你需要改变内容,把JS放在底部。PS:使用jQuery更容易。现在就试试。我没有时间学习新的语言,我一整天都在尝试,你的例子不起作用,一个也不起作用。现在你告诉我,我应该去学习一个新的框架来解决这个简单的问题。没有thx。我建议你学习jQuery,你不会后悔的。我给你举了个例子。