通过jQuery添加JSF标签/组件 [英] Adding JSF tags/components via jQuery

查看:86
本文介绍了通过jQuery添加JSF标签/组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所做的:

var a = $(document.getElementById("panelForm:tableId01"));
$("<p:outputLabel value='Testing'/>").appendTo(a);

它不显示标签,但是例如,如果我这样做,它将起作用:

It does not show the label but if I did this, for example, it works:

$("<font color='red'>Red</font>").appendTo(a);

推荐答案

您似乎完全错过了JSF和JavaScript上下文的知识.

You seem to have completely missed the point of JSF and the context of JavaScript.

JSF基本上是HTML代码生成器.要亲自查看它,请创建一个(简单且有效的)JSF页面,然后在您喜欢的Web浏览器中将其打开.右键单击并选择查看源代码.你看到了什么?是的,这是一个HTML代码!如果JSF正确完成了工作,则由于Web浏览器无法理解它们,很简单的原因,您将看不到任何JSF标记.它只了解HTML.

JSF is basically a HTML code generator. To see it yourself, create a (simple and working) JSF page and open it in your favourite webbrowser. Rightclick and choose View Source. What do you see? Yes, it's one and all HTML code! If JSF has done its job right, you should not see any JSF tags, for the very simple reason that the webbrowser do not understand them. It understands only HTML.

JavaScript是一种客户端语言,它在Web浏览器而不是Web服务器中运行.作为证据,当您在物理上不同的计算机上运行webserver和webbrowser并调用JavaScript onclick="alert('peek-a-boo')"时,您会在webbrowser中看到它,而不是在webserver中. JavaScript可以看到HTML DOM树(任何通过document对象可用的东西,例如document.getElementById("someId")). JavaScript无法看到负责生成该HTML的原始服务器端源代码,更不用说执行它了.对于jQuery,它并没有什么不同,原因很简单,因为它是一个基于JavaScript的库.

JavaScript is a client side language which runs in webbrowser and not in webserver. As evidence, when you run webserver and webbrowser at physically different machines and you invoke JavaScript onclick="alert('peek-a-boo')", then you see it in webbrowser, not in webserver. JavaScript can see the HTML DOM tree (anything which is available via document object, such as document.getElementById("someId")). JavaScript can not see original server side source code who's responsible for generating that HTML, let alone execute it. For jQuery it is not different for the simple reason that it's a JavaScript based library.

您需要以不同的方式解决您的具体功能要求.如果要动态添加JSF组件,则应该通过JSF本身而不是通过JavaScript来完成.这是显示多种方式之一的答案:如何动态添加JSF组件 .但是,如果您坚持要为此使用jQuery,那么您应该自己指定JSF组件的HTML输出,但是您基本上仍然完全缺少JSF的要点.然后仔细阅读以下链接.

You need to solve your concrete functional requirement differently. If you want to dynamically add JSF components, then you should be doing it via JSF itself, not via JavaScript. Here's an answer showing one of the many ways: How to dynamically add JSF components. If you however insist in using jQuery for this, then you should be specifying the JSF component's HTML output yourself, but you're basically still completely missing the point of JSF. Carefully read the below link then.

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆