[英]Dynamically changing width of div with Javascript
Here 's the test site. 这是测试站点。
I'm using a Javascript onClick event function to dynamically change the src of the displayed full-size image. 我正在使用Javascript onClick事件函数动态更改显示的全尺寸图像的src。 However, this isn't quite working the same way in changing the width of the div that contains the image.
但是,在更改包含图像的div的宽度时,这并不是完全相同的方法。 I have tested that the function is actually getting the given width and it is, so it's probably just not defining the width value of my div correctly.
我已经测试过该函数实际上正在获取给定的宽度,因此它可能只是未正确定义div的宽度值。
<?php
$username = $_GET['username'];
session_start();
$_SESSION['username'] = $username;
//Database Information
$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";
//Connect to database
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
//Do the query
$query = mysql_query("SELECT * FROM images ORDER BY idnum DESC LIMIT 5");
//Generate an array of all images
$images = array();
while($image = mysql_fetch_array($query)) {
//this adds each image to the images array
$images[] = $image;
$image['filename'] = $firstimage;
}
?>
<?php
// Beginning attempt at a caption script.
?>
<html>
<head>
<title>Home - Site in Development</title>
<link rel="stylesheet" type="text/css" href="styles.css"/>
<script type="text/javascript">
// Switches the url to view large image.
function switchImageUrl(url, width) {
document.getElementById('img-frame').src = url;
document.GetElementById('center_frame').offsetwidth = width;
}
</script>
</head>
<body onLoad="CalculateAllImageWidthes()">
<div id='account_links'>
<?php
if ($_SESSION['username']) {
echo "Welcome $username!";
} else { ?>
<a href='login.php'>Login</a> | <a href='register.php'>Register</a>
<?php } ?>
</div>
<h1>Picture Captions</h1>
<br/>
<br/>
<div id="left_bar">
Submit a picture <a href="upload.php">here</a>.
<hr/>
<h2>Top Images</h2>
<br/>
<div id="front_pg_images">
<?php foreach($images as $image) { ?>
<a href="#" onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>')"><img src="<?php echo $image['filename'];?>" width="72px" height="58px" id="front_pg_thumbnail"/></a>
<?php echo $image['name']." - by ".$image['submitter']; ?> <br/>
<br/>
<?php } ?>
</div>
</div>
<div id="center_frame" width="<?php echo $image['width']; ?>">
<img src="<?php echo $image['filename'];?>" name="default" id="img-frame" align="left" valign="top">
</div>
This is the problematic line: 这是有问题的行:
document.GetElementById('center_frame').offsetwidth = width;
getElementById
- JavaScript is case-sensitive getElementById
-JavaScript区分大小写 offsetwidth
(which should have been offsetWidth
anyway) with style.width
offsetwidth
(这应该是offsetWidth
反正)与style.width
width
with width + 'px'
width + 'px'
代替width
so your line looks like: 所以你的行看起来像:
document.getElementById('center_frame').style.width = width + 'px';
代替.offsetwidth = width
使用.style.width = width;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.