0

我想知道是否可以在 JavaScript 函数内或使用 jQuery 创建 Struts2 标记(例如)。

例如,我试过:

function createStrutsTag(){           

        var newElement;
        newElement= document.createElement("div");
        newElement.className = 'newer';
        newElement.innerHTML = '<s\:textfield label="HELLO" \/>';              

        newElement.css('visibility','visible'); }

但是,它只是创建了包含标签 < s:textfield label="HELLO" /> 的 div,而没有被解析为它的真正含义。

我还尝试了struts2-jquery-plugin 标签(sj),它也崩溃了......所以我想知道它是否可以完成,或者我应该在正文的 HTML 部分中创建这些元素,被 CSS 样式隐藏和然后在应用程序需要时使用 JavaScript /jquery 函数显示或隐藏它们。

4

1 回答 1

2

JS is executed on the client. JSP tags are evaluated on the server.

You have several options, not limited to:

Use the output of the text tag as a JS value

There are at least two ways to do this:

  • Put the JS in a JSP, or
  • Evaluate your JS via the JSP processor

Evaluate some JS in the JSP and call the external JS

For example, you could create a hash of I18N labels etc. in your JSP and pass it to JS functionality that lives in a JS file (where it should live).

Store the output of the text tag in a hidden DOM element

Retrieve it using JS and construct your DOM as you are now.

This is arguably the cleanest.


Rough overview of "clean" solution using jQuery

See this fiddle for the basics.

JSP:

<div id="hello" style="display: none;"><s:text label="HELLO" /></div>

JS:

function createDiv() {
  var $div = $("<div className='newer'>").html($("#hello").html());
  $("#outputHere").html($div);
}

It could be tightened up a bit, but that's the general idea.

于 2013-05-29T15:37:08.503 回答