在 Javascript 函数调用上显示图像!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6327631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying an image on Javascript function call !
提问by HackToHell
I am a javascript noob and I need help with this problem !
我是一个 javascript 菜鸟,我需要帮助解决这个问题!
On a specific condition a function is called and when it is called , I want an image to be displayed ! How do I do that ? this is not working!
在特定条件下调用一个函数,当它被调用时,我想要显示一个图像!我怎么做 ?这是行不通的!
<script type="text/javascript">
function _enabled() {
document.write("<img border="0" src="http://4.bp.blogspot.com/-C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/some image.png" />");
}
var r =true;
</script>
This is not working , I get this error
这不起作用,我收到此错误
Error: missing ) after argument list
Source File: file:///C:/Users/Gowtham/Desktop/new%20%202.html
Line: 5, Column: 33
Source Code:
document.write("<img border="0" src="http://4.bp.blogspot.com/-C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/sjs.png" />");
回答by T.J. Crowder
The syntax error is that you're using double quotes for your document.write
string literal, but you've also used a double quotes your HTML attributes:
语法错误是您对document.write
字符串文字使用了双引号,但您还对 HTML 属性使用了双引号:
document.write("<img border="0" src="http://4.bp.blogspot.com/-C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/some image.png" />");
// ^--- here and other places
That double quote ends the string literal, so you get a syntax error. You'd want to escape that with a backslash, or use JavaScript's handy feature where you can use single quotes around string literals.
该双引号结束字符串文字,因此您会收到语法错误。你想用反斜杠来转义它,或者使用 JavaScript 的方便特性,你可以在字符串文字周围使用单引号。
But that alone isn't going to solve the problem. If you call document.write
after the main parsing of the page is finished, you're going to completely erase the page and replaceit with new content.
但仅凭这一点并不能解决问题。如果document.write
在页面的主要解析完成后调用,您将完全擦除页面并用新内容替换它。
To display an image after the main parsing of the page is complete, you create a new img
element via the DOM document.createElement
function and then append it to the element in which you want it to appear. For instance, this code puts a new image at the end of the document:
要在页面的主要解析完成后显示图像,您可以img
通过 DOMdocument.createElement
函数创建一个新元素,然后将其附加到您希望它出现的元素上。例如,此代码在文档末尾放置一个新图像:
function addTheImage() {
var img = document.createElement('img');
img.src = "http://....";
document.body.appendChild(img);
}
To add to another element, rather than the end of body
, you just need to get a reference to the element. There are various ways to do that, including document.getElementById
to look it up by its id
value, document.getElementsByTagName
to look up all elements with a given tag, etc. Some browsers offer very rich methods like querySelectorAll
that let you use CSS selectors, but not all do yet. (Many JavaScript libraries like jQuery, Prototype, YUI, Closure, or any of several othersplug that gap for you, though, and offer other handy utility features, and features smoothing over browser inconsistencies and outright browser bugs.)
要添加到另一个元素,而不是结尾body
,您只需要获取对该元素的引用。有多种方法可以做到这一点,包括document.getElementById
按id
值document.getElementsByTagName
查找、查找具有给定标签的所有元素等。一些浏览器提供了非常丰富的方法,例如querySelectorAll
让您使用 CSS 选择器,但并非所有方法都这样做。(不过,许多 JavaScript 库,如jQuery、Prototype、YUI、Closure或其他几个库中的任何一个都为您填补了这一空白,并提供了其他方便的实用功能,以及平滑浏览器不一致和浏览器错误的功能。)
More about the dom:
更多关于 dom:
- The DOM2 Core specification(widely supported by major browsers)
- The DOM2 HTML specification(DOM stuff specific to HTML, also fairly widely supported)
- The DOM3 Core specificiation(less widely supported, but things are improving)
- 该DOM2核心规范(由主流浏览器的广泛支持)
- 该DOM2 HTML规范(DOM东西具体到HTML,也相当广泛支持)
- 该DOM3核心故障排除基本(少广泛的支持,但情况正在好转)
回答by Raptor
Escape your document.write codes.
转义您的 document.write 代码。
<script type="text/javascript">
function _enabled() {
document.write("<img border=\"0\" src=\"http://4.bp.blogspot.com/-C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/some%20image.png\" />");
}
var r =true;
</script>
回答by Jigar Joshi
<script type="text/javascript">
function _enabled() {
document.write("<img border='0' src='http://4.bp.blogspot.com/- C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/some image.png' />");
}
var r =true;
</script>
Note: Single quotes.
注意:单引号。
Tested works fine.
经测试工作正常。
回答by James Allardice
You need to escape your quote marks, or use single quote marks inside the string passed to document.write
. For example "<img border=\"0\"...
or <img border='0'...
您需要对引号进行转义,或在传递给 的字符串中使用单引号document.write
。例如"<img border=\"0\"...
或<img border='0'...
At the moment, your string ends at the start of your first attribute.
目前,您的字符串在您的第一个属性的开头结束。
回答by cypher
You either need to escape quotes or use single quotes for javascript and double for html (for example).
您要么需要转义引号,要么对 javascript 使用单引号,对 html 使用双引号(例如)。
<script type="text/javascript">
function _enabled() {
document.write('<img border="0" src="http://4.bp.blogspot.com/-C4vvMEB9MyI/TfW0lduV2NI/AAAAAAAAAZc/N7HL1pUusGw/s1600/some image.png" />');
}
var r =true;
</script>