我已经在其他网络应用程序中看到过这种情况,但我对Javascript还不熟悉,并且无法自己解决这个问题.我想以编程方式创建Google Hangout.但是,在官方API中,您可以创建环聊的唯一方法是在页面上添加环聊按钮.
我想要做的是让用户点击另一个用户的img并以这种方式打开环聊.似乎我可以捕获该点击,然后在后台"点击"环聊按钮.但是,我对Javascript不是很好,所以我不知道如何实现这一点.
编辑
我在下面尝试了mohamedrais的解决方案但没有运气.它与按钮的类有关吗?
以下是该按钮的代码,取自环聊文档并添加了id:
<script type="text/javascript" src="https://apis.google.com/js/platform.js"></script>
<div class="g-hangout" id="hangout-giLkojRpuK"
data-render="createhangout">
</div>
Run Code Online (Sandbox Code Playgroud)
这是我添加的JS,用ids 渲染:
<script>
window.onload = function() {
var userImage = document.getElementById("img-giLkojRpuK");
var hangoutButton = document.getElementById("hangout-giLkojRpuK");
userImage.onclick = function() {
console.log("in onclick");
hangoutButton.click(); // this will trigger the click event
};
};
</script>
Run Code Online (Sandbox Code Playgroud)
当我单击时img,"on onclick"被记录,因此函数被调用.但是,没有任何反应 - 环聊未启动.
当我直接点击环聊按钮时,它会启动环聊.但是,我想最终隐藏它并从图像中执行单击.
以下是上方环聊按钮的图片img:

moh*_*ias 22
window.onload = function() {
var userImage = document.getElementById('imageOtherUser');
var hangoutButton = document.getElementById("hangoutButtonId");
userImage.onclick = function() {
hangoutButton.click(); // this will trigger the click event
};
};
Run Code Online (Sandbox Code Playgroud)
这将成功
小智 5
虽然这个问题比较老,但这是一个答案:)
你所要求的可以通过使用jQuery的实现。点击()事件方法 和。对()事件方法
所以这可能是代码:
// Set the global variables
var userImage = $("#img-giLkojRpuK");
var hangoutButton = $("#hangout-giLkojRpuK");
$(document).ready(function() {
// When the document is ready/loaded, execute function
// Hide hangoutButton
hangoutButton.hide();
// Assign "click"-event-method to userImage
userImage.on("click", function() {
console.log("in onclick");
hangoutButton.click();
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
96906 次 |
| 最近记录: |