Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 页面选项卡和JS:getElementbyId为';不行吗?_Javascript_Php_Tabs - Fatal编程技术网

Javascript 页面选项卡和JS:getElementbyId为';不行吗?

Javascript 页面选项卡和JS:getElementbyId为';不行吗?,javascript,php,tabs,Javascript,Php,Tabs,我试图在php代码中实现完整的页面标签。我只有一个问题:带有getElementbyId的脚本部分似乎不起作用 它基本上应该激活包含id=“defaultOpen”的按钮,因此浏览器已经打开了一个选项卡。您可以在上面的示例中看到逻辑 我仔细检查了一切(尤其是回声),看不出我在这里犯了什么错误 以下是脚本: <script> function openPage(pageName, elmnt, color) { // Hide all elements with class="tabc

我试图在php代码中实现完整的页面标签。我只有一个问题:带有
getElementbyId
的脚本部分似乎不起作用

它基本上应该激活包含id=“defaultOpen”的按钮,因此浏览器已经打开了一个选项卡。您可以在上面的示例中看到逻辑

我仔细检查了一切(尤其是回声),看不出我在这里犯了什么错误

以下是脚本:

<script>
function openPage(pageName, elmnt, color) {
// Hide all elements with class="tabcontent" by default */
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
}

// Remove the background color of all tablinks/buttons
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
    tablinks[i].style.backgroundColor = "";
}

// Show the specific tab content
document.getElementById(pageName).style.display = "block";

// Add the specific color to the button used to open the tab content
elmnt.style.backgroundColor = color;
}

// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>

既然单击任何按钮都会调用函数
openPage
,为什么不在第一次加载时调用
openPage
函数,而不是尝试模拟按钮单击来执行相同的操作?换言之,改变这一点:

document.getElementById("defaultOpen").click();
为此:

openPage('Kader', document.getElementById("defaultOpen"), 'green');
document.getElementById("defaultOpen").click();
openPage('Kader', document.getElementById("defaultOpen"), 'green');