Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何参考';这';来自JSF组件中的javascript调用?_Javascript_Jsf - Fatal编程技术网

如何参考';这';来自JSF组件中的javascript调用?

如何参考';这';来自JSF组件中的javascript调用?,javascript,jsf,Javascript,Jsf,在前面的简化示例中,Javascript函数中使用的“this”关键字不会引用生成的A HREF元素,而是引用全局窗口,因为JSF生成以下(简化)代码: 因此,由于JSF将用户定义的onclick封装在一个函数中,这将指向全局窗口。我没有访问元素id的权限,因为我的代码用于可以在循环等中使用的通用组件,并且我没有使用支持bean(Facelets+JSF) 所以我的问题是,有没有一种方法可以在不知道id的情况下从onclick javascript函数中访问a HREF元素 var a=函数

在前面的简化示例中,Javascript函数中使用的“this”关键字不会引用生成的A HREF元素,而是引用全局窗口,因为JSF生成以下(简化)代码:


因此,由于JSF将用户定义的onclick封装在一个函数中,这将指向全局窗口。我没有访问元素id的权限,因为我的代码用于可以在循环等中使用的通用组件,并且我没有使用支持bean(Facelets+JSF)

所以我的问题是,有没有一种方法可以在不知道id的情况下从onclick javascript函数中访问a HREF元素

var a=函数(){alert(this)};var b=function(){if(typeof jsfcljs=='function'){jsfcljs(document.forms['mainForm'],'generatedId,action,action','';}返回false};返回(a()==false)?假:b()

哦,亲爱的上帝!那太可怕了

不,它似乎在你有机会看到它之前就扔掉了“this”——它应该说“return(a.call(this)==false)?false:b()

在IE上,您只能从window.event读取src元素,但这非常难看

通过使用不引人注目的脚本,完全避免JSF奇怪的事件破坏如何?省略onclick并说:

<a onclick="var a=function(){alert(this)};var b=function(){if(typeof jsfcljs == 'function'){jsfcljs(document.forms['mainForm'],'generatedId,action,action','');}return false};return (a()==false) ? false : b();" href="#" id="generatedId">text</a>
如果需要链接继续进入JSF的事件处理,则必须保存旧的onclick事件处理程序并在函数末尾调用它,或者使用Element.addEventListener(IE上的attachEvent)在onclick之前的阶段将脚本挂钩放入

var a=函数(){alert(this)};var b=function(){if(typeof jsfcljs=='function'){jsfcljs(document.forms['mainForm'],'generatedId,action,action','';}返回false};返回(a()==false)?假:b()

哦,亲爱的上帝!那太可怕了

不,它似乎在你有机会看到它之前就扔掉了“this”——它应该说“return(a.call(this)==false)?false:b()

在IE上,您只能从window.event读取src元素,但这非常难看

通过使用不引人注目的脚本,完全避免JSF奇怪的事件破坏如何?省略onclick并说:

<a onclick="var a=function(){alert(this)};var b=function(){if(typeof jsfcljs == 'function'){jsfcljs(document.forms['mainForm'],'generatedId,action,action','');}return false};return (a()==false) ? false : b();" href="#" id="generatedId">text</a>
如果您需要链接继续进入JSF的事件处理,您必须保存旧的onclick事件处理程序并在函数末尾调用它,或者使用Element.addEventListener(IE上的attachEvent)在onclick之前的阶段将脚本挂钩放入其中。

编辑:

转到此处获取从id获取clientId的小型库/示例代码:


JSF控件发出的HTML ID使用名称空间来避免冲突(例如,该控件是UIData的子控件并多次发出,或者容器是一个容器,因此可以在一个HTML页面中多次呈现视图)。此ID称为clientId,与JSF控件上设置的ID不同

如果希望在视图中发出clientId,可以使用托管bean来执行

document.getElementById('generatedId').onclick= function() {
    alert(this);
    return false; // if you want to not follow the link
}
公共类JavaScriptBean{
公共字符串getOnclickButton1(){
字符串id=“button1”;
字符串clientId=getClientId(id);
返回“警报('您单击了元素id=“+clientId+”);”;
}
公共字符串getOnclickButton2(){
String id=“button2”;
字符串clientId=getClientId(id);
返回客户ID;
}
私有字符串getClientId(字符串id){
FacesContext context=FacesContext.getCurrentInstance();
UIViewRoot视图=context.getViewRoot();
UIComponent=find(视图,id);
返回component.getClientId(上下文);
}
专用UIComponent查找(UIComponent,字符串id){
if(id.equals(component.getId())){
返回组件;
}
迭代器kids=component.getFacetsAndChildren();
while(kids.hasNext()){
UIComponent kid=kids.next();
UIComponent found=find(kid,id);
if(found==null){
继续;
}
发现退货;
}
返回null;
}
}
这是在应用程序的中配置的,并使用表达式语言绑定到视图:

public class JavaScriptBean {

    public String getOnclickButton1() {
        String id = "button1";
        String clientId = getClientId(id);
        return "alert('You clicked element id=" + clientId + "');";
    }

    public String getOnclickButton2() {
        String id = "button2";
        String clientId = getClientId(id);
        return clientId;
    }

    private String getClientId(String id) {
        FacesContext context = FacesContext.getCurrentInstance();
        UIViewRoot view = context.getViewRoot();
        UIComponent component = find(view, id);
        return component.getClientId(context);
    }

    private UIComponent find(UIComponent component, String id) {
        if (id.equals(component.getId())) {
            return component;
        }

        Iterator<UIComponent> kids = component.getFacetsAndChildren();
        while (kids.hasNext()) {
            UIComponent kid = kids.next();
            UIComponent found = find(kid, id);
            if (found == null) {
                continue;
            }
            return found;
        }

        return null;
    }

}

编辑:

转到此处获取从id获取clientId的小型库/示例代码:


JSF控件发出的HTML ID使用名称空间来避免冲突(例如,该控件是UIData的子控件并多次发出,或者容器是一个容器,因此可以在一个HTML页面中多次呈现视图)。此ID称为clientId,与JSF控件上设置的ID不同

如果希望在视图中发出clientId,可以使用托管bean来执行

document.getElementById('generatedId').onclick= function() {
    alert(this);
    return false; // if you want to not follow the link
}
公共类JavaScriptBean{
公共字符串getOnclickButton1(){
字符串id=“button1”;
字符串clientId=getClientId(id);
返回“警报('您单击了元素id=“+clientId+”);”;
}
公共字符串getOnclickButton2(){
String id=“button2”;
字符串clientId=getClientId(id);
返回客户ID;
}
私有字符串getClientId(字符串id){
FacesContext context=FacesContext.getCurrentInstance();
UIViewRoot视图=context.getViewRoot();
UIComponent=find(视图,id);
返回component.getClientId(上下文);
}
专用UIComponent查找(UIComponent,字符串id){
if(id.equals(component.getId())){
返回组件;
}
迭代器kids=component.getFacetsAndChildren();
while(kids.hasNext()){
UIComponent kid=kids.next();
UIComponent found=find(kid,id);
if(found==null){
继续;
}
发现退货;
}
返回null;
}
}
这在应用程序中配置并绑定到t
<head>
..
<script type="text/javascript">
var myCommandLinkId = "undefined";
</script>
..
</head>

..

<h:commandLink id="myCommandLink" value="Click Me" onmousedown="myCommandLinkId=this.id;" onclick="alert(myCommandLinkId);" />