1

只是看看是否有人对我在 Optimizely 的变体代码部分中的以下代码更清晰有任何建议:

window.scrollBox();
$("#related-posts").css({"z-index":"1"});
$("#optslidebox").replaceWith("<div id=\"optslidebox\" style=\"z-index: 2; right: -999px;\">\n\t<a class=\"close\"></a>\n\t<p>Recommended</p>\n\t<h2>WHO ARE YOUR FAVORITE RAPPERS' FAVORITE RAPPERS RIGHT NOW?</h2>\n</div>");
$("#optslidebox > h2").wrapInner("<a href=\"http://greenlabel.com/sound/rappers-who-skate-skaters-who-rap/\"></a>");

这就是我想附加到正文的内容(可以在 Optimizely 中编辑 html,但它非常混乱):

<div id="optslidebox">
    <a class="close"></a>
    <p>Recommended</p>
    <h2><a href="#">WHO ARE YOUR FAVORITE RAPPERS' FAVORITE RAPPERS RIGHT NOW?</a></h2>
</div>

我想知道如何将 div 存储到一个变量中,如果有意义的话,还有 optslidebox div 中的项目。我也有兴趣了解如何实现所有变量以使它们显示在正文中。

提前致谢!

4

1 回答 1

1

而不是replaceWith你可以使用html,因为看起来你只是替换了innerHtml. 我们喜欢用数组连接来清理它,但仍然保持循环完整

所以有了这种模式,它看起来像

/* _optimizely_evaluate=force */ /*global $*/
window.variationHtml = [
  '<a class="close"></a>',
  '<p>Recommended</p>',
  '<h2><a href="#">WHO ARE YOUR FAVORITE RAPPERS\' FAVORITE RAPPERS RIGHT NOW?</a></h2>',
].join('');
/* _optimizely_evaluate=safe */
$("#optslidebox").html(variationHtml);

如果事情变得更加激烈,尽管我们构建了一个多行模板函数来帮助不需要对所有内容进行数组连接。

/* _optimizely_evaluate=force */ /*global $*/

// timpl | https://github.com/clearhead/timpl
!function(n){"use strict";function r(n){var r=i.exec(n.toString());if(!r)throw new TypeError("Multiline comment missing.");return r[1]}function t(n,r){return n.replace(c,function(n,t){for(var i=t.split("."),o=i.length,s=r,u=0;o>u;u++){if(s=s[i[u]],s===e)throw'tim: "'+i[u]+'" not found in '+n;if(u===o-1)return s}})}n.timpl=function(n,e){return t(n.call?r(n):n,e||{}).replace(/^\s+|\s+$/g,"")};var e,i=/\/\*!?(?:\@preserve)?[ \t]*(?:\r\n|\n)([\s\S]*?)(?:\r\n|\n)[ \t]*\*\//,o="{{",s="}}",u="[a-z0-9_$][\\.a-z0-9_]*",c=new RegExp(o+"\\s*("+u+")\\s*"+s,"gi")}(window);

window.variationHtml = timpl(function () { /*
  <a class="close"></a>
  <p>Recommended</p>
  <h2><a href="#">WHO ARE YOUR FAVORITE RAPPERS' FAVORITE RAPPERS RIGHT NOW?</a></h2>
*/});

/* _optimizely_evaluate=safe */
$("#optslidebox").replaceWith(window.variationHtml);
于 2015-07-17T17:58:16.083 回答