直接上代码
这是子页面的一部分代码由于我要实现聊天功能,多以需要父页面调用子页面chat变量判断是否需要弹窗
<div class="container">
<header><h3 id="username">与{{username}}聊天中</h3></header>
<div id="result">
<div class="box" v-for="(message,index) in Message">
<div class="mess" :class="message.id"><img v-show="message.id != id" id="otherHead" alt="head" :src= "otherHead"/>{{message.text}}<img v-show="message.id == id"alt="head" id="head" :src= "head"/></div>
</div>
</div>
<footer>
<textarea type="text" id="msgData" rows="5" class="form-control"></textarea>
<input class="btn btn-danger btn-sm del-btn" type="button" id="send" value="发送">
</footer>
</div>
<script>
var chat = 0;
var id = localStorage.getItem('id');
var toId = getParam('toId');
这是父页面的界面,我用的是iframe作为子页面 src动态设置这里就略过了直接来重点
<table width="1138px" height="960px" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="100%">
<iframe name="content" id="iframe" src="QueryPic.html" width="100%" height="100%" frameborder="0" scrolling="none">
</iframe>
</td>
</tr>
<div class="hello" hidden></div>
</table>
父窗口调用子窗口的chat变量(这里的chat需要是全局变量)
if(obj.toId == id && (null == $('#iframe').eq(0)[0].contentWindow.chat || null == window.frames["content"].chat)){
layer.open({
type: 2
,title:"收到新消息"
,offset: 'rb'
,shade: 0
,time: 5000
,closeBtn: false
,area: '100px,100px;'
,anim: 4
,content: ['/msg/msgController/init.htm?toId=' + obj.id, 'no']
,btnAlign: 'c'
,moveType: 0 //拖拽模式,0或者1
,btn: ['知道了','打开']
,btn2:function () {
$('iframe').attr("src",baseUrl + '/msg/msgController/init.htm?toId=' + obj.id);
},success:function () {
$('#remark').focus();
}
})
}
这里可以看到
$(’#iframe’).eq(0)[0].contentWindow.chat (通过id获取)
window.frames[“content”].chat(通过name获取)
都能获得chat的值
我这里做的是个兼容
(新手上路,欢迎指教)