简体   繁体   English

发送HTML到javascript函数

[英]Send HTML to javascript function

I've created a javascript function to displays a dialog and receives two parameters for title and body, which will be called from links in a slider. 我创建了一个javascript函数来显示一个对话框,并接收两个用于title和body的参数,这将从滑块中的链接调用。 The thing is I want the body to be able to display HTML code, so you can display links or whatever. 问题是我希望主体能够显示HTML代码,因此您可以显示链接或其他内容。 All of this inside a velocity template used in a Liferay portal (just in case it came useful). 所有这些都在Liferay门户中使用的速度模板中(以防万一它有用)。 This is what i have now: 这就是我现在所拥有的:

JS : JS

function displayPopup(title, body) {
        $("#more-popup").html(body);
        $("#more-popup").dialog({
            modal: true,
            title: title
        });
        return false;
    }

VELOCITY : 速度

#if ($slide.displayPopup.data == true)
    #set( $popupBody = $escapeTool.html($slide.popupBody.data) )
    <a href="#" class="moreButton" onclick="displayPopup('$slide.popupTitle.data', '$slide.popupBody.data')">$moreTextString</a>
#elseif ($slide.Link.data)
    <a href="$slide.Link.data" class="moreButton">$moreTextString</a>
#end

I can't come up with a workaround to this. 我无法解决此问题。 First the html was wrong because of the quotes. 首先,由于引号,html是错误的。 After using escaping for HTML entities, the link seems ok, but a javascript exception jumps when clicking on the link: Uncaught exception: Unexpected Identifier Also i don't how to convert back the code to HTML that i can insert in the dialog. 在对HTML实体使用转义之后,该链接看起来还可以,但是单击该链接时会跳出一个javascript异常: Uncaught exception: Unexpected Identifier另外,我也不会将代码转换回可以插入对话框的HTML。 Thanks in advance. 提前致谢。

A not-so-good solution is to try this: 一个不太好的解决方案是尝试此操作:

#if ($slide.displayPopup.data == true)
    #set( $popupBody = $escapeTool.html($slide.popupBody.data) )
    <a href="#" class="moreButton" onclick="displayPopup('$slide.popupTitle.data', "$slide.popupBody.data")">$moreTextString</a>
#elseif ($slide.Link.data)
    <a href="$slide.Link.data" class="moreButton">$moreTextString</a>
#end

Notice the " instead of '. 注意“而不是”。

But the real problem is that you are using an HTML escape function where you need a JS escape function that just escapes ' with \\' for example... Using HTML escape can give you problems when using your dialog (that you don't see yet because of the JS error). 但是真正的问题是,您正在使用HTML转义函数,而您需要一个JS转义函数,例如,该转义符仅对'带\\'进行转义...使用HTML转义会在使用对话框时给您带来问题(您看不到但由于JS错误)。

I don't know how to do this in Velocity but you can start by replacing ' with \\' 我不知道如何在Velocity中执行此操作,但您可以先将'替换为\\

I finally fixed it with a workaround but not actually passing the HTML to the function, but creating the div to display the popup for each slide that needs it. 我最终通过一种解决方法对其进行了修复,但实际上并未将HTML传递给该函数,而是创建了div以显示需要它的每张幻灯片的弹出窗口。

Velocity 速度

#if ($slide.displayPopup.data == true)
    <a id="more-popup-button-$velocityCount" href="#" class="moreButton popup-button" >$moreTextString</a>
    <div id="more-popup-$velocityCount" class="more-popup" title="$slide.popupTitle.data">
        $slide.popupBody.data
    </div>
#elseif ($slide.Link.data)
    <a href="$slide.Link.data" class="moreButton">$moreTextString</a>
#end    

JS JS

$("#slides a.moreButton.popup-button").click(function (event) {
    var number = $(this).attr("id");
    number = number.substr(number.lastIndexOf("-") + 1);
    $("#more-popup-" + number).dialog({
        modal: true
    });
    event.preventDefault();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM