简体   繁体   English

内联Javascript未执行

[英]Inline Javascript isn't executed

I want to set the current time to an Element with momentjs. 我想用momentjs将当前时间设置为Element。

<span onload="$(this).text(moment().format('MMMM Do YYYY, h:mm:ss a'));"></span>

What is wrong with it? 这有什么问题? Nothing is shown. 没有显示任何内容。

onload is not what you need (it won't even work), you need to be executing this on document.ready onload不是你需要的(它甚至不起作用),你需要在document.ready上执行它

I think you're mixing jQuery and JavaScript, try using document.ready in a script block. 我认为你正在混合使用jQuery和JavaScript,尝试在脚本块中使用document.ready

First we'll give your span an ID 首先,我们将为您的跨度提供ID

<span id="yourSpan"></span>

Then we can do: 然后我们可以这样做:

<script type="text/javascript">
    $(function () {
        $("#yourSpan").text(moment().format('MMMM Do YYYY, h:mm:ss a'));
    });
</script>

EDIT: As you've said you want it inline (although it's really bad practice and I'll have to wash my hands after writing it), still add your ID to your span as above, but do this: 编辑:正如你所说,你想要内联(虽然这是非常糟糕的练习,我写完后我必须洗手),仍然按照上面的标准添加你的ID,但是这样做:

<body onload="$('#yourSpan').text(moment().format('MMMM Do YYYY, h:mm:ss a'));">

onload is not supported in the span tag , some tags that support onload : span标记中不支持onload,一些支持onload的标记:

<body>, <frame>, <frameset>, <iframe>, <img>, 
<input type="image">, <link>, <script>, <style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM