我将我的JS文件jquery.min放在我的网站标记之前。 如何用简单的JavaScript替换这里的jQuery代码?
function generateScript()
{
?>
<script type="text/javascript">
$(document).ready(function(e) {
<?php
foreach($this->modules AS $m) {
echo ' $("#kategoria_'.$m.'").show() ; '."\n";
}
?>
});
</script>
<?php
}
我想要,因为如果我将所有JS文件包含到标记中,我会得到js错误,因为这个生成器代码是在我调用js文件之前,而$ function将不存在。
更新
现在,我用这个:
function generateScript()
{
?>
<script type="text/javascript">
//$(document).ready(function(e) {
<?php
foreach($this->modules AS $m) {
//echo ' $("#kategoria_'.$m.'").show() ; '."\n";
echo 'document.getElementById("#kategoria_'.$m.'").style.display = "block"; '."\n";
}
?>
//});
</script>
<?php
}
它生成此站点站点源:
document.getElementById("#kategoria_szamitastechnika-1").style.display = "block";
document.getElementById("#kategoria_szamitastechinkai-periferiak-2").style.display = "block";
document.getElementById("#kategoria_laptop-notebook-tablet-3").style.display = "block";
document.getElementById("#kategoria_terkovek-kerti-lapok-13").style.display = "block";
document.getElementById("#kategoria_szorakoztato-elektronika-18").style.display = "block";
document.getElementById("#kategoria_konzol-es-pc-jatekszoftver-19").style.display = "block";
document.getElementById("#kategoria_asztali-szamitogep-20").style.display = "block";
document.getElementById("#kategoria_kellekek-kiegeszitok-21").style.display = "block";
document.getElementById("#kategoria_vezetek-nelkuli-eszkozok-22").style.display = "block";
document.getElementById("#kategoria_lego-38").style.display = "block";
document.getElementById("#kategoria_irodai-butorok-57").style.display = "block";
控制台出错:
未捕获的TypeError:无法读取属性&#39; style&#39;为null
答案 0 :(得分:1)
function generateScript()
{
?>
<script type="text/javascript">
(function() {
<?php
foreach($this->modules AS $m) {
echo ' document.getElementById("kategoria_'.$m.'").style.display = "block"; '."\n";
}
?>
})();
</script>
<?php
}
您可以将$(document).ready(function(){...});
替换为本机JS代码:
(function() {
})();
答案 1 :(得分:1)
你试试这个。
function generateScript()
{
?>
<script type="text/javascript">
<?php
foreach($this->modules AS $m) {
echo ' document.getElementById("kategoria_'.$m.'").style.display = "block";\n';
}
?>
</script>
<?php
}
答案 2 :(得分:0)
document.getElementById()不需要符号#,然后:
替换document.getElementById("#kategoria_szamitastechnika-1")
document.getElementById("kategoria_szamitastechnika-1")