<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>
使用jQuery:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
<script language="javascript">
function onlyNumber(str) {
str = str.toString();
return str.replace(/\D/g, '');
}
$(document).ready(function() {
var widthx = onlyNumber($("#testx").css("width")) * 1;
$("#mycontent").css("width", (widthx + 45) + "px");
});
</script>
<style type="text/css">
#testx
{
width: 950px;
}
#mycontent
{
width: 800px; //this width needs to change from javascript to 950 + 45 px
}
</style>
</head>
<body>
<!-- YOUR HTML HERE -->
</body>
</html>
嘗試在兩個'widthx'和'widthx.width'做'console.log'或'alert' - 這將幫助你找到你的問題。 – jball
會發生什麼? 'style.width'將是一個字符串,如果你想執行數學運算,你必須先將它轉換爲數字。 –
有趣的是,我知道,但我不知道語法 – JarM