调用 PHP 变量作为函数值
Calling PHP variable as function value
我继承了我们sharepoint网站上的一段代码,基本看懂了,但我迷失在PHP中。
代码是手风琴:https://jqueryui.com/accordion/
它采用值 1 或 0 来确定默认显示哪个。
我希望这个值每天都在变化(忽略月末),所以我做了一个函数,如果月份是奇数或偶数,则设置 1 或 0。
我的问题是使用我函数的输出作为另一个函数的值。
我想我需要做一些连接,但我对 JQuery 开始和 PHP 结束的地方感到困惑
下面的代码不可复制,因为它还由两个额外的 SharePoint webparts 提供,它们生成每个手风琴片段的内容,所以我只包含代码的内容。
Look for $checkActive this is used in my function and is my desired value for the final lines
jQuery(document).ready(function($) {
var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;
if (inDesignMode == "1")
{
}
else
{
//Put the WebPart Title for all the Web Parts you wish
//to put into the jQuery UI Accordion into the array below.
//Accordian(["MY TO-DO LIST","MY COLLABORATION","MY TEAMSITES"]);
//Accordian(["MY FAVORITES","MY TEAMSITES","MY COLLABORATION"]);
Accordian(["MY COLLABORATION","IN THE NEWS"]);
}
});
function Accordian(webPartTitles)
{
for(index in webPartTitles)
{
var title = webPartTitles[index];
$("#accordian").append('<h3>'+title+'</h3>');
var addedToAccordion = 'false';
$("span:contains('"+title+"')").each(function(){
if ($(this).html() == title && addedToAccordion == 'false' ){
if($(this).closest("span").closest("[id^='MSOZoneCell_WebPart']").contents().length > 0)
{
$(this).closest("span").hide().closest("[id^='MSOZoneCell_WebPart']").contents().appendTo($("#accordianTemp"));
addedToAccordion = 'true';
$("span:contains('"+title+"Link')").each(function(){
if ($(this).html() == title + "Link"){
if($(this).closest("span").closest("[id^='MSOZoneCell_WebPart']").contents().length > 0)
{
$(this).closest("span").hide().closest("[id^='MSOZoneCell_WebPart']").contents().appendTo($("#accordianTemp"));
}
}
});
}
}
});
$("#accordian").append("<div>" + $("#accordianTemp").html() + "</div>");
$("#accordianTemp").empty();
}
$dw = date( "j", time());
$checkActive = ($dw % 2 == 0) ? '1' : '0'; // If the day number is Odd, we will show My Collaboration
the active value below is what normally accepts 1 or 0 but I want it to take my variable value
$("#accordian").find("div").remove( ".ms-webpart-chrome-title" );
$("#accordian").accordion({ heightStyle: "content" }, {active:$checkActive });
}</script>
您需要用 PHP 开始和结束标记包围您的 PHP 并在 Javascript / jQuery 中使用 PHP 值,它需要得到回应。这些是适用的部分:
<?php
$dw = date( "j", time());
$checkActive = ($dw % 2 == 0) ? '1' : '0';
?>
和
$("#accordian").accordion({ heightStyle: "content" }, {active: "<?php echo $checkActive; ?>"});
我尝试了 MattyF 和 Venkat 的建议,但我无法让它工作,我最终做的是单独使用 Java 创建一个解决方案。
var day = new Date().getDate();
var checkActive;
if (day % 2 == 0) { // If the day number is Odd, we will show My Collaboration
checkActive = 1;
} else {
checkActive = 0;
}
$("#accordian").find("div").remove( ".ms-webpart-chrome-title" );
$("#accordian").accordion({ heightStyle: "content" }, {active: checkActive});
我在简单地尝试传递一个我设置的不包含任何逻辑的变量后得出了这个结论。当使用 java VAR 时,构建 IF 是一件很快的事情。正如我在问题中所说,我是这些语言的新手,所以当我第一次看到 $ 我假设 PHP,尽管我后来意识到这只是在 JQuery...
中调用 THIS
感谢您的帮助,并为我的无知道歉
我继承了我们sharepoint网站上的一段代码,基本看懂了,但我迷失在PHP中。 代码是手风琴:https://jqueryui.com/accordion/ 它采用值 1 或 0 来确定默认显示哪个。
我希望这个值每天都在变化(忽略月末),所以我做了一个函数,如果月份是奇数或偶数,则设置 1 或 0。
我的问题是使用我函数的输出作为另一个函数的值。 我想我需要做一些连接,但我对 JQuery 开始和 PHP 结束的地方感到困惑
下面的代码不可复制,因为它还由两个额外的 SharePoint webparts 提供,它们生成每个手风琴片段的内容,所以我只包含代码的内容。
Look for $checkActive this is used in my function and is my desired value for the final lines
jQuery(document).ready(function($) {
var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;
if (inDesignMode == "1")
{
}
else
{
//Put the WebPart Title for all the Web Parts you wish
//to put into the jQuery UI Accordion into the array below.
//Accordian(["MY TO-DO LIST","MY COLLABORATION","MY TEAMSITES"]);
//Accordian(["MY FAVORITES","MY TEAMSITES","MY COLLABORATION"]);
Accordian(["MY COLLABORATION","IN THE NEWS"]);
}
});
function Accordian(webPartTitles)
{
for(index in webPartTitles)
{
var title = webPartTitles[index];
$("#accordian").append('<h3>'+title+'</h3>');
var addedToAccordion = 'false';
$("span:contains('"+title+"')").each(function(){
if ($(this).html() == title && addedToAccordion == 'false' ){
if($(this).closest("span").closest("[id^='MSOZoneCell_WebPart']").contents().length > 0)
{
$(this).closest("span").hide().closest("[id^='MSOZoneCell_WebPart']").contents().appendTo($("#accordianTemp"));
addedToAccordion = 'true';
$("span:contains('"+title+"Link')").each(function(){
if ($(this).html() == title + "Link"){
if($(this).closest("span").closest("[id^='MSOZoneCell_WebPart']").contents().length > 0)
{
$(this).closest("span").hide().closest("[id^='MSOZoneCell_WebPart']").contents().appendTo($("#accordianTemp"));
}
}
});
}
}
});
$("#accordian").append("<div>" + $("#accordianTemp").html() + "</div>");
$("#accordianTemp").empty();
}
$dw = date( "j", time());
$checkActive = ($dw % 2 == 0) ? '1' : '0'; // If the day number is Odd, we will show My Collaboration
the active value below is what normally accepts 1 or 0 but I want it to take my variable value
$("#accordian").find("div").remove( ".ms-webpart-chrome-title" );
$("#accordian").accordion({ heightStyle: "content" }, {active:$checkActive });
}</script>
您需要用 PHP 开始和结束标记包围您的 PHP 并在 Javascript / jQuery 中使用 PHP 值,它需要得到回应。这些是适用的部分:
<?php
$dw = date( "j", time());
$checkActive = ($dw % 2 == 0) ? '1' : '0';
?>
和
$("#accordian").accordion({ heightStyle: "content" }, {active: "<?php echo $checkActive; ?>"});
我尝试了 MattyF 和 Venkat 的建议,但我无法让它工作,我最终做的是单独使用 Java 创建一个解决方案。
var day = new Date().getDate();
var checkActive;
if (day % 2 == 0) { // If the day number is Odd, we will show My Collaboration
checkActive = 1;
} else {
checkActive = 0;
}
$("#accordian").find("div").remove( ".ms-webpart-chrome-title" );
$("#accordian").accordion({ heightStyle: "content" }, {active: checkActive});
我在简单地尝试传递一个我设置的不包含任何逻辑的变量后得出了这个结论。当使用 java VAR 时,构建 IF 是一件很快的事情。正如我在问题中所说,我是这些语言的新手,所以当我第一次看到 $ 我假设 PHP,尽管我后来意识到这只是在 JQuery...
中调用 THIS感谢您的帮助,并为我的无知道歉