简体   繁体   English

如何在JS中循环CDATA字段(Foreach)

[英]How to loop CDATA Fields in JS (Foreach)

I have a list of widgets in wordpress. 我在wordpress中有一个小部件列表。 One of those widgets is a slider. 这些小部件之一是滑块。 As the slider needs the ID and other information, I am parsing a cdata array to the website using the wp_localize_script - one for every slider. 由于滑块需要ID和其他信息,因此我正在使用wp_localize_script解析一个cdata数组到网站-每个滑块一个。 So in my site, right after the footer element, i have the following code (example) 所以在我的网站中,在footer元素之后,我有以下代码(示例)

<script type='text/javascript'>
/* <![CDATA[ */
var sliderID = {"slider":"57d7b035941e9"};
var sliderID = {"slider":"57d7b03596340"};
/* ]]> */
</script>

Each number is a php "uniqid();"-number to set a unique ID for the slider. 每个数字都是一个php“ uniqid();”-编号,用于设置滑块的唯一ID。 I am also loading a "sliderscripts.js" to the footer if the slider is active in the current site (in the footer, but below the CDATA). 如果滑块在当前站点中处于活动状态(在页脚中,但在CDATA下方),我还将在页脚中加载“ sliderscripts.js”。 In the sliderscripts.js I now want to make a slider foreach ID. 现在,我要在sliderscripts.js中为每个ID制作一个滑块。 Using the ID is no problem like 使用ID就像

$(document).ready(function () {
    $('#'+sliderID.slider).owlCarousel({
... my options ...
});

Now the question: How can I, using JS, loop that? 现在的问题是:如何使用JS进行循环? If it would be 如果会的话

<script type='text/javascript'>
/* <![CDATA[ */
var sliderID = {"slider":"57d7b035941e9","slider":"57d7b03596340"};
/* ]]> */
</script>

there wouldnt be a problem. 不会有问题。 I need to foreach CDATA with var SliderID do something. 我需要用var SliderID foreach CDATA做一些事情。 Would be great if you help me! 如果您能帮助我,那就太好了!

thank you! 谢谢!

Even if @CBroe had a great idea, it was way too much work to start by 0. For others with the same problem, this is my solution: 即使@CBroe有个好主意,也要从0开始做太多工作。对于其他有相同问题的人,这是我的解决方案:

1. Generate ID 1.生成ID

$sid = uniqid(); // sid = slider-ID - FE: 123

will generate a unique ID for every slider 将为每个滑块生成唯一的ID

2. Generate Password 2.产生密码

As I've been told not to use uniqid multiple times in a function (it should work but it seems to be a bad technique?) I am using the wordpress hook generate password to generate an ID-similar element. 有人告诉我不要在函数中多次使用uniqid(它应该可以工作,但这似乎是一种不好的技术吗?)我正在使用wordpress钩子生成密码来生成ID相似的元素。

$ID2 = wp_generate_password(20, false, false); // generates an password / ID with 20 digits. FE: 12345678912345678911

3. Add ID and data-attribut to the code 3.在代码中添加ID和数据属性

I now open the owlslider code like 我现在打开owlslider代码,例如

<div id="slider_<?php echo $sid; ?>" class="owls" data-id2="<?php echo $ID2; ?>"

4. Localize Script 4.本地化脚本

Now I am also localizing these values, so I can get them with JS 现在我也在本地化这些值,所以我可以用JS来获取它们

$slidersettings = array(
    'slider' => $sid,
    'url' => plugins_url(),
);
$sliderhandle = 'sl_'.$ID2; // will be sl_12345678912345678911

wp_localize_script( 'slider', $sliderhandle, $slidersettings );

5. Build Slider. 5.构建滑块。

Last but not least I enqueue a custom script where I check for the IDs to set every slider like this 最后但并非最不重要的一点是,我加入了一个自定义脚本,在其中检查ID以设置每个滑块,例如

// check for all div's with the class "owls" AND id starts with "slider"
$('div.owls[id^="slider"]').each( function() { 
    var $div = $(this);
    var ID2 = $div.data('ID2'); // Now I grab the ID2 / 2nd ID out of the data-attribute


var cdata = window['sl_' + ID2]; // this now tells me in which CDATA Array I have to look for my values

$install= $( '#slider_' + cdata.slider); // slider_123 <- 123 = uniqid.

        $install.owlCarousel({
... standard owl stuff here...
})

thats it. 而已。 Hope this will help someone else sometime.... all the best 希望这会在某个时候帮助别人。

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

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