本文介绍了如何避免使用此内联Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个wordpress网站.在single.php
下,我具有以下body
标记
I have a wordpress site. Under single.php
, I have the following body
tag
<body <?php body_class(); ?> onLoad="func(<?php echo $thePostID?>);" >
在网上阅读文章使我坚信避免使用内联CSS和内联javascipt.因此,我对网站进行了重组,以便现在将样式和脚本包含在外部文件中.除了这一行代码,因为它确实需要the post id
,而且我不知道如何在single.php之外检索它.
Reading articles on the web made me convinced of avoiding inline CSS and inline javascipt. So I made a restructuring of my site so that styles and scripts are contained now in external files. Except for this line of code since it really need the post id
and I dont know how can I retrieve it outside of single.php.
感谢您的一贯帮助.
推荐答案
使用属性:
<body data-post-id="<?php echo $thePostID?>">
然后您可以写
var postId = document.body.getAttribute('data-post-id');
这篇关于如何避免使用此内联Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!