如何从JS创建的子页面替换父页面中的字符串?

时间:2017-02-16 19:03:02

标签: javascript replace

我正在用JS

创建一个动态列表
var obj = JSON.parse(Agent212);
var i;
var serie = "Agent212";
for (i = 0; i < obj.strips.length; i++) {
    if (obj.strips[i].Collectie == "0") {
        document.write("<li><a onclick='newPage(" + obj.strips[i].Nummer + "," + serie + ")' style = 'color:orange'>" + obj.strips[i].Nummer + " - " + obj.strips[i].Titel + "</a></li>");
    }
    else if (obj.strips[i].Collectie == "2") {
        document.write("<li><a onclick='newPage(" + obj.strips[i].Nummer + "," + serie + ")' style = 'color:red'>" + obj.strips[i].Nummer + " - " + obj.strips[i].Titel + "</a></li>");
    }
    else {
        document.write("<li><a class='ToHide' onclick='newPage(" + obj.strips[i].Nummer + "," + serie + ")'><strike>" + obj.strips[i].Nummer + " - " + obj.strips[i].Titel + "</strike></a></li>");
    }
}

当我点击列表中的某个项目时,我创建了一个子页面

 function newPage(StripNum, SerieNaam) {
     var obj = JSON.parse(SerieNaam);
     var i;
     var x = StripNum;
     for (i = 0; i < obj.strips.length; i++) {
         if (obj.strips[i].Nummer == x) {
             if (obj.strips[i].Collectie == 0) {
                 var myWindow = window.open();
                 myWindow.document.writeln('<html>');
                 myWindow.document.writeln('<head>');
                 myWindow.document.writeln('<meta name="viewport" content="width=device-width, initial-scale=1">');
                 myWindow.document.writeln('<link rel="stylesheet" href="./css/W3CSS.css">');
                 myWindow.document.writeln('<scr' + 'ipt>');
                 myWindow.document.writeln('function windowClose(){');
                 myWindow.document.writeln('window.close();}'); **
                 myWindow.document.writeln('function ToevoegenVerzameling(){'); **
                 myWindow.document.writeln('window.alert("toegevoegd aan verzameling");');
                 myWindow.document.writeln('window.close();');
                 myWindow.document.writeln('}');
                 myWindow.document.writeln('</scr' + 'ipt>');
                 myWindow.document.writeln('</head>');
                 myWindow.document.writeln('<body>');
                 myWindow.document.writeln('<div class="w3-container w3-display-topmiddle">');
                 myWindow.document.writeln('<div class="w3-card-16 w3-round-xlarge" style="width:100%">');
                 myWindow.document.writeln('<a onclick="windowClose();"><center><img src="' + obj.strips[i].Link + obj.strips[i].Nummer + '.jpg"></center></a>');
                 myWindow.document.writeln('<div class="w3-container w3-margin-top">');
                 myWindow.document.writeln('<center><B>Nummer: </B><I>' + obj.strips[i].Nummer + '</I></center>');
                 myWindow.document.writeln('<center><B>Titel: </B><I>' + obj.strips[i].Titel + '</I></center>');
                 myWindow.document.writeln('<center><button onclick="ToevoegenVerzameling()" class="w3-btn w3-light-grey w3-border w3-border-black w3-round-xlarge w3-margin-top w3-margin-bottom" style="width:100%"><h2><b>Toevoegen</b></h2></button></center>');
                 myWindow.document.writeln('</div>');
                 myWindow.document.writeln('</div>');
                 myWindow.document.writeln('</div>');
                 myWindow.document.writeln('</body>');
                 myWindow.document.writeln('</html>');
             }

以上所有内容确实有效,但我知道我想向ToevoegenVerzameling()添加替换功能。

我尝试添加这段代码。

myWindow.document.writeln('if (window.opener != null && !window.opener.closed){');
myWindow.document.writeln('var str = "<li><a onclick='
    newPage(" + obj.strips[i].Nummer + ", " + serie + ")
    ' style = '
    color: orange '>" + obj.strips[i].Nummer + " - " + obj.strips[i].Titel +"</a></li>";');
myWindow.document.writeln('str.replace("<a onclick='
        newPage(", " < a class = 'ToHide'
            onclick = 'newPage(");'); myWindow.document.writeln('str.replace("style = '
            color: orange '>", "><\strike>");'); myWindow.document.writeln('str.replace("</a>", "</strike></a>");}');

但我无法让它发挥作用,我是否在这犯了错误?

0 个答案:

没有答案