var iframe = this.iframe.contentWindow;
时间: 2024-02-22 20:51:55 浏览: 15
这段代码的作用是获取当前页面中的iframe元素,并将其contentWindow属性赋值给变量iframe。这个变量可以用于操作iframe中的文档内容,例如修改文本或者执行JavaScript代码等。
```javascript
var iframe = this.iframe.contentWindow;
```
相关问题
QString script = "var iframe = document.activeElement;" "var iframeDoc = iframe.contentWindow.document;" "var attrValue = iframeDoc.activeElement.Name;" "attrValue;"; //page()->runJavaScript(script, [&eTagName2](const QVariant &v) { eTagName2 = v.toString(); }); page()->runJavaScript(script, [this](const QVariant& result) { QString attrValue = result.toString(); // ????????? });返回invalid值
问题可能在于您正在尝试访问 iframe 内部的 `activeElement` 属性,但是 iframe 中并没有元素被选中。这会导致 JavaScript 返回 `undefined` 或 `null`,从而导致 QVariant 转换为 QString 时得到 `invalid` 值。
您可以在运行 JavaScript 之前检查 iframe 是否具有焦点元素,例如:
```cpp
page()->runJavaScript("var iframe = document.activeElement;"
"var iframeDoc = iframe.contentWindow.document;"
"var activeElem = iframeDoc.activeElement;"
"activeElem ? activeElem.Name : null;",
[this](const QVariant& result) {
QString attrValue = result.toString();
if (attrValue == "null") {
// 没有选中元素
} else {
// 选中元素的 Name 属性值为 attrValue
}
});
```
如果 iframe 中有选中元素,将返回该元素的 `Name` 属性值。否则,返回 `null`。
// var agent, password, mode, phoneId //登录所需参数 // window.onload = function () { // init(); // }; //初始化电话条 function init() { var iframe = document.createElement('iframe'); iframe.name = "uinCall"; iframe.id = "uinCall"; iframe.ref = "uinCall"; iframe.allow = "microphone"; iframe.src = "https://ykf.uincall.com/telephonebarplugin/index.html"; // iframe.src = "https://ceshi.uincall.com/telephonebarplugin/index.html"; var telephoneBar = document.getElementById("telephoneBar"); if (telephoneBar) { telephoneBar.appendChild(iframe); var uinCall = document.getElementById("uinCall"); console.log('@@@-', uinCall); iframe.style.width = telephoneBar.getAttribute("data-width"); iframe.style.height = telephoneBar.getAttribute("data-height"); iframe.style.border = 0; } } //打印来自电话条内的抛出事件 window.addEventListener('message', function (e) { // console.log('@EchoData','message>>',e) console.info(e); if (e.data) { try { var resData = JSON.parse(e.data); // 动态修改iframe的高度 if (resData && resData.key && resData.key === 'changeCallHistoryState') { var telephoneBar = document.getElementById("telephoneBar"); var uinCall = document.getElementById("uinCall"); var frameHeight = telephoneBar.getAttribute("data-height"); if (resData.data) { // 显示 var heightConfig = parseInt(frameHeight); uinCall.style.height = (!heightConfig || heightConfig < 310) ? '310px' : heightConfig + 'px'; } else { uinCall.style.height = frameHeight; } } } catch (err) { console.info(e); } } }); //高级话务 function telephoneFunction(data) { let uincall = document.getElementById('uinCall'); if (uincall) { uincall.contentWindow.postMessage(data, '*'); } else { init(); uincall = document.getElementById('uinCall'); if (uincall) { setTimeout(() => { uincall.contentWindow.postMessage(data, '*'); }, 500); } } } 用vue优化代码编程
你可以使用Vue来优化你的代码编程。首先,你需要将你的代码包装在一个Vue实例中。你可以在Vue的`created`生命周期钩子中初始化电话条,并在`message`事件监听器中处理来自电话条的抛出事件。然后,你可以在Vue实例的方法中调用`telephoneFunction`来发送消息给电话条。
下面是一个示例代码:
```javascript
new Vue({
el: '#app',
created() {
this.init();
},
methods: {
init() {
var iframe = document.createElement('iframe');
iframe.name = "uinCall";
iframe.id = "uinCall";
iframe.ref = "uinCall";
iframe.allow = "microphone";
iframe.src = "https://ykf.uincall.com/telephonebarplugin/index.html";
var telephoneBar = document.getElementById("telephoneBar");
if (telephoneBar) {
telephoneBar.appendChild(iframe);
var uinCall = document.getElementById("uinCall");
console.log('@@@-', uinCall);
iframe.style.width = telephoneBar.getAttribute("data-width");
iframe.style.height = telephoneBar.getAttribute("data-height");
iframe.style.border = 0;
}
},
handleMessage(e) {
console.info(e);
if (e.data) {
try {
var resData = JSON.parse(e.data);
if (resData && resData.key && resData.key === 'changeCallHistoryState') {
var telephoneBar = document.getElementById("telephoneBar");
var uinCall = document.getElementById("uinCall");
var frameHeight = telephoneBar.getAttribute("data-height");
if (resData.data) {
var heightConfig = parseInt(frameHeight);
uinCall.style.height = (!heightConfig || heightConfig < 310) ? '310px' : heightConfig + 'px';
} else {
uinCall.style.height = frameHeight;
}
}
} catch (err) {
console.info(e);
}
}
},
telephoneFunction(data) {
let uincall = document.getElementById('uinCall');
if (uincall) {
uincall.contentWindow.postMessage(data, '*');
} else {
this.init();
uincall = document.getElementById('uinCall');
if (uincall) {
setTimeout(() => {
uincall.contentWindow.postMessage(data, '*');
}, 500);
}
}
}
},
mounted() {
window.addEventListener('message', this.handleMessage);
},
beforeDestroy() {
window.removeEventListener('message', this.handleMessage);
}
});
```
在这个示例中,我们将Vue实例绑定到`#app`元素上,并在`created`生命周期钩子中初始化电话条。`handleMessage`方法用于处理来自电话条的抛出事件。`telephoneFunction`方法用于发送消息给电话条。在`mounted`钩子中添加了`message`事件监听器,在`beforeDestroy`钩子中移除了事件监听器,以确保在组件销毁之前清理事件监听器。