如何使用JavaScript获取参考元素? [英] How get reference element with JavaScript?
问题描述
我在前端开发方面经验很少,只说过Web ui不在我的星球上..
I have little experience with front-end development, needles to mention that web ui is out of my planet..
我需要在按钮的onClick事件上显示和隐藏元素的问题. 技术栈是带有PrimeFaces的JSF,对于我们使用JavaScript的脚本.
The problem I need to show and hide an element, on the onClick event of a button. The technology stack is JSF with PrimeFaces, for the script we use JavaScript.
基本上-如何获得对我需要显示/隐藏的元素的引用? 而且您能给我一个线索,为什么注释中的行(下面)很好用?
Basically - how can I get reference to the element that I need to show/hide? And can you give me a clue why the line (below) in comment works good?
function test(id, id2){
var element =$(document).get("#testEl");
$(element).show(); //this doesn't work??? although I get no errors executing it..
// $("#tab-view\\:template-form\\:testEl").show(); // <-- however this works? Why?
$(btn).css("display", "none");
}
html:
<p:commandButton widgetVar="edit" id="edit" value="Edit" onclick="test(this, #testEl)" type="button"/>
<p:panel widgetVar="opa" id="testEl" closable="false" visible="false" >
<h:panelGrid ..>....
</h:panelGrid>
</p:panel>
谢谢!
推荐答案
正如Kuba所建议的……使我感到困惑的是,JSF组合物(将容器命名为表单和组合物)嵌套"了它们的名称,基于一个属性prependId值.
As Kuba suggested ... what caused the my confusion was that JSF compoments (naming containers as forms and composition) "nest" their names, based on an attribute prependId value..
因此,我感兴趣的元素的客户端ID是compositionID:formID:elID. 我可以通过以下方式获得它:
So the client-side ID of the element of my interest is compositionID:formID:elID. And I could get it as:
function test(btn, wv){
$(PF(wv).jqId).show();
$(btn).css("display", "none");
}
这篇关于如何使用JavaScript获取参考元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!