如何在 HTML 中使用内联 JavaScript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7050586/
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
How to use inline JavaScript in HTML?
提问by Saeed Neamati
Support that I have this hidden field:
支持我有这个隐藏字段:
<input type='hidden' id='handler' name='handler' value='5534882394' />
And imagine that I fetch an HTML fragment from server via jQuery AJAX.
并想象我通过 jQuery AJAX 从服务器获取 HTML 片段。
<div id='result'>
<span></span>
</div>
However, instead of writing something like this:
但是,而不是写这样的东西:
$(function(){
$('#result span').text($('#handler').val());
});
I'd like to use something like:
我想使用类似的东西:
<div id='result'>
<span>javascript:$('#handler').val();</span>
</div>
However, I don't get the intended result. Can I use this approach with JavaScript?
但是,我没有得到预期的结果。我可以在 JavaScript 中使用这种方法吗?
Update:Everybody please, I know about $(document).ready();
. So, don't provide better ways. I just wanted to know if it's possible to have inline JavaScript, or not, and if yeah, how?
更新:请大家,我知道$(document).ready();
。所以,不要提供更好的方法。我只是想知道是否可以使用内联 JavaScript,如果可以,如何使用?
回答by Guffa
No, you can't use that approach with Javascript. There is no such thing as inline Javascript.
不,您不能在 Javascript 中使用这种方法。没有内联 Javascript 之类的东西。
What you see in a link like <a href="javascript:alert(1)">
is the javascript:
pseudo-protocol, similar in use to the http:
protocol. When you point the browser to such an URL, the browser runs the script.
您在链接中看到的<a href="javascript:alert(1)">
是javascript:
伪协议,类似于协议的使用http:
。当您将浏览器指向这样的 URL 时,浏览器会运行该脚本。
If you want to run a script in the page, you need a script tag.
如果要在页面中运行脚本,则需要一个脚本标记。
回答by ipr101
You could do -
你可以这样做——
<input type="text" id="handler" value="yes"/>
<div id='result'>
<span>
<script>document.write($('#handler').val());</script>
</span>
</div>
and the page would run the Javascript when it hit the script tag. You'd have to make sure that '#handler' element was already loaded though or the script would fail.
并且页面会在点击脚本标签时运行 Javascript。您必须确保已加载“#handler”元素,否则脚本将失败。
In general it would be better to keep script tags such as this away from your mark-up.
一般来说,最好让这样的脚本标签远离你的标记。
Working demo - http://jsfiddle.net/ipr101/h6zXs/
回答by emboss
Although the <script>
tag works and would let you do what you are trying to do, I would really suggest rethinking your design there. This is not directly what you would call "unobtrusive Javascript".
尽管<script>
标签有效并且可以让您做您想做的事情,但我真的建议您在那里重新考虑您的设计。这不是您所说的“不显眼的 Javascript”。
Why do you preferto mix HTML and JS? If it's for curiosity - OK, but if this is intended to turn into production code then you might want to separate the two as much as you can. You will be rewarded with a much better maintainability in the end.
为什么你更喜欢混合 HTML 和 JS?如果是出于好奇 - 好的,但是如果这是为了变成生产代码,那么您可能希望尽可能地将两者分开。最终,您将获得更好的可维护性。
回答by Matt Ball
Have you forgotten about the <script>
tag?
你忘了的<script>
标签?
<div id="result"></div>
<script>$('#result').text($('#handler').val();</script>
回答by Malvolio
A few years back, I had that exact problem: I was using AJAX to retrieve HTML and that HTML had embedded Javascript. I was never able to find a better solution than parsing the HTML manually to dig out the Javascript tags and calling eval
on their contents. Hackish and unreliable, but it was the best I could find.
几年前,我遇到了那个确切的问题:我使用 AJAX 来检索 HTML,而 HTML 已经嵌入了 Javascript。我再也找不到比手动解析 HTML 以挖掘 Javascript 标签并调用eval
其内容更好的解决方案了。骇人听闻且不可靠,但这是我能找到的最好的。