我希望每1000毫秒将我的元素obj替换为函数myPlugin创建的新元素。
问题是元素没有被替换。
我想这个问题来自于披露或类似的东西。早上6点我懒得再想了,我只想知道答案:)
柱塞:https://plnkr.co/edit/J9k6kFc0yWySXKFwBdB1?p=preview
脚本
<script type="text/javascript">
$(function () {
$('.myCrypto').each(function(i, obj) {
window.setInterval(function() {
var a = myPlugin(obj.getAttribute("cf_widget_size"), obj.getAttribute("cf_widget_from"), obj.getAttribute("cf_widget_to"), obj.getAttribute("cf_clearstyle"));
obj.replaceWith(a);
}, 1000);
});
});
正文
<div>
<div class="myCrypto" cf_widget_size="large" cf_widget_from="BTC" cf_widget_to="usd" cf_clearstyle=true></div>
</div>myPlugin
function myPlugin( cf_widget_size, cf_widget_from, cf_widget_to, cf_clearstyle) {
var t = document.scripts[document.scripts.length - 1],
n = t.parentElement,
r = document.createElement("iframe"),
i = "https://www.worldcoinindex.com/widget/renderWidget?size=" + cf_widget_size + "&from=" + cf_widget_from + "&to=" + cf_widget_to + "&clearstyle=" + cf_clearstyle;
if (cf_widget_size == "small") {
r.width = "300px";
r.height = "135px"
} else if (cf_widget_size == "medium") {
r.width = "300px";
r.height = "240px"
} else if (cf_widget_size == "large") {
r.width = "300px";
r.height = "340px"
}
r.id = "cf_widget_iframe"
r.setAttribute("data-size", cf_widget_size);
r.style.cssText = "border:none;"; -1 == navigator.userAgent.indexOf("MSIE") ? r.src = i : r.location = i;
console.log("hahah");
return (r);
};发布于 2017-12-28 06:14:12
您必须用新的obj objcect替换a
obj.replaceWith(a);
obj = a;发布于 2017-12-28 06:23:39
这里有几个问题-
setInterval在$.each里面。这样,选择器只执行一次,然后期望setInterval函数中的元素每1000 ms执行一次,这将在第一次迭代中被替换。myPlugin应该将类.myCrypto分配给它返回的元素,以便可以再次使用相同的类选择器来选择它。cf_widget_size=large和html中提到的其他属性应该重新分配给要返回的元素(iframe)。看看这把小提琴我想说什么。https://jsfiddle.net/nikhilgirraj/jv0h1gLm/
https://stackoverflow.com/questions/48002024
复制相似问题