我的网页上有一个svg。当右键单击时,元素应该显示一个上下文菜单,其中包含从数据库中提取的数据。元素的编写方式使得右键单击事件在页面上触发一个js函数。我需要从java脚本中调用上下文菜单。有人能帮我做这个吗。我已经被这个问题困扰了将近3天。要素:
<rect/><text>L</text></g><g id="118" onmousedown="RightClickExecute(event,118)">
java脚本:
function RightClickExecute(event, id) {
if (event.button == 2) {document.getElementById("myForm:selectedEntityid").value = id;
document.getElementById("myForm:selectedObjectType").value = 'Entity';
document.getElementById("myForm:RightAction").click();
}
}
function showContextMenu(){
document.getElementById("myForm:contextMenuItemId").click();
}
XHTML:
<p:contextMenu id="contextMenuId" for="svgContainerPanel"
widgetVar="contextMenuVar" rendered="#{myBean.objectType=='Entity' ? true : false}">
<p:menuitem id="contextMenuItemId" ></p:menuitem>
</p:contextMenu>
<p:contextMenu event="click" id="contextMenu2Id" for="contextMenuId"
widgetVar="contextMenu2Var" model="#{my.model}" >
</p:contextMenu>
<p:commandButton id="RightAction" style="visibility:hidden"
action="#{myBean.populateMenu}" ajax="true"
type="submit" oncomplete="showContextMenu()"
update="contextMenuId,contextMenu2Id">
</p:commandButton>
<h:inputHidden id="selectedEntityid"
value="#{myBean.selectedEntityId}">
</h:inputHidden>
<h:inputHidden id="selectedObjectType"
value="#{myBean.objectType}">
</h:inputHidden>
Primefaces的contextMenu没有获取该选项的选项,所以您可以使用jquery来实现这一点。如果你想显示contextMenu,你必须将contextMenu的位置更改为Mouse的位置(默认情况下页面加载contextMenu,但它有css显示:none,所以你需要更改css)。Primefaces的contextMenu有一个在客户端中使用的widgetvar属性(它有方法show来显示它)。
Primefaces's contextMenu
有widgetvar
属性在客户端中使用(它有方法show来显示它)。
例如,当鼠标悬停在一个id为rongnk
的组件上时,您会显示上下文菜单,我有一个表单(id:form),一个上下文菜单(id:xxx)
<script type="text/javascript">
//<![CDATA[
$(document).on('mouseover', '#form\:rongnk', function(e) {
$(PrimeFaces.escapeClientId('form:xxx')).css({
top: e.pageY+'px',
left: e.pageX+'px'
}).show();
});
//]]>
</script>