2010-08-01 25 views
0

喜逢身体我想通过点击一个按钮,在新窗口中打开图像,使用JavaScript,然后设置一个类的图像打开一个元素,使用此代码:我不能在一个类分配给新的窗口

OpenWindow=window.open("", "newwin","height=100,width=1000"); 
    OpenWindow.document.write("<img id='img1' src='webfonts.jpg'>"); 
    OpenWindow.document.getElementById("img1").className = "larger"; 
    OpenWindow.document.close(); 

,这是内部样式我已经包含在我在样式标记代码: .larger {宽度:10px的; } 和在身体标记 1:我具有由IMG标签包括我的图像源 2:使一个按钮标签与一个onclick =较大()属性

但不幸的是我与最后一个句子中的问题,该班级根本没有分配给图像。 我曾尝试使用classname而不是setttribute,但它会生成相同的结果。 有什么想法? thx

+0

你忘了包括代码:)添加它,选择它,然后单击1和0的按钮对其进行格式化:) – 2010-08-01 10:59:41

+1

见http://stackoverflow.com/questions/2490627/how-can-i-reliable-set-the-class-attr-w-javascript-on-ie-ff-chrome -etc – 2010-08-01 11:02:25

+0

当然,我已将图像源以这一行写入新窗口: OpenWindow.document.write(“”); – loll 2010-08-01 11:06:24

回答

2

首先,你写的代码是错误的。 img后面有一个撇号。其次,您没有为您的img元素分配ID,因此您无法通过document.getElementById访问该元素。

使用类似

var docHead = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'+ 
    'http://www.w3.org/TR/html4/strict.dtd">'+ 
    '<html><head><title>Test page</title>'+ 
    '<style type="text/css">'+ 
    ' .larger { width: 10px }'+ 
    '</style></head><body>'; 
var docFoot = '</body></html>'; 

OpenWindow=window.open("", "newwin", "height=100,width=1000"); 
OpenWindow.document.write(docHead); 
OpenWindow.document.write('<img id="img1" src="webfonts.jpg">'); 
OpenWindow.document.write(docFoot); 
OpenWindow.document.close(); 
OpenWindow.document.getElementById('img1').className = "larger"; 
+0

plz重读代码我已经为我的图像分配了一个ID: 在身体 – loll 2010-08-01 11:33:28

+0

我试过你的代码 – loll 2010-08-01 11:34:00

+0

但它不工作,听它似乎更好,如果我发送你一个附加文件,但我是一个新用户,你可以告诉我该怎么做。 – loll 2010-08-01 11:35:00

0

在附注中,您还需要在此新页面中包含一些css,否则此类=“更大”将不会执行任何操作。

OpenWindow.document.write('<html><link href="css/style.css"/><body>'); 
OpenWindow.document.write('<img src="{source URL}" id="img1" class="larger" />'); 
OpenWindow.document.write('</body></html>'); 
+0

首先非常感谢您的快速响应,但我已将该课程作为内部样式包含在我的页面中,这是完整的代码: <脚本类型= “文本/ JavaScript的”> 函数较大() { \t \t OpenWindow.document.write( ''); OpenWindow.document.write(''); OpenWindow.document.write(''); } <按钮的onclick = “大()”>较大 – loll 2010-08-01 11:30:24

+0

<按钮的onclick = “大()”>较大 – loll 2010-08-01 11:31:44

+0

resize image <脚本类型= “文本/ JavaScript的”> 函数较大() { \t \t OpenWindow.document.write( ''); OpenWindow.document.write(''); OpenWindow.document.write(''); } <按钮的onclick = “大()”>较大 – loll 2010-08-01 11:32:00

相关问题