简体   繁体   English

Javascript读取HTML标签

[英]Javascript read HTML tags

I have a problem and I don't know how to fix it after sometime now, I really need help 我有问题,现在不知道如何解决,我真的需要帮助

So I have asp:GridView , in my DB i am saving text (In English column) with HTML tags, on DataBound event I am using Context.Server.HtmlDecode(encoded); 所以我有asp:GridView ,在我的数据库中,我正在使用Context.Server.HtmlDecode(encoded);保存文本(英文列),在DataBound事件上,我正在使用Context.Server.HtmlDecode(encoded); so it is displayed perfectly 所以它显示完美

这是网格视图

But problem is, that also on that event, I am creating OnClick event for that cell ... calling javascript function, and sending parameters to it, it looks like this 但是问题是,同样在该事件上,我正在为该单元格创建OnClick事件...调用javascript函数,并向其发送参数,看起来像这样

ChangeTranslationText('English',
'<div style="text-align: center;"><span style="font-style:italic;">Stavi dole
</span><span style="font-weight: bold;">neki text<br><br><br>Hvala na paznji!
<br><br>Mozes Z da stavis<br><br>Ali Z<br><br>Hvala<br></span></div> '
,'56');return false;

Now in function I am trying that my javascript read that HTML and format my text as it looks like in GridView ... 现在在功能上,我正在尝试让我的javascript读取HTML并格式化文本,使其在GridView中看起来像...

function ChangeTranslationText(Language, Text, Control) {

        document.getElementById("MainContent_txtChangeLanguage").value = Language;

        var el = $('<div></div>');
        el.html(Text);
        document.getElementById("MainContent_txtTranslationText").innerHTML = el.text();

        document.getElementById("MainContent_hfControl").value = Control;

        $('#ModalChangeTranslate_Fade').modal('show');
    }

And I can't format it as I don't know how ... this is best I got so far 而且我无法格式化,因为我不知道...到目前为止,这是我最好的

在此输入图像描述

Can you help me and advice me how can Javascript read that HTML and keep format like it is in GridView cell? 您能帮助我并为我提供建议吗,JavaScript如何在GridView单元格中读取HTML并保持其格式?

I made a test here and saw two things: 我在这里进行了测试,发现了两件事:

var el = $('<div></div>'); was throwing $ is not defined . $ is not defined You probably have jquery library in your project, but you actually dont need to declare this el variable. 您的项目中可能有jquery库,但是实际上不需要声明此el变量。 You can use it like you did before: 您可以像以前一样使用它:

document.getElementById("MainContent_txtTranslationText").innerHTML = Text;

Another thing was your Text parameter. 另一件事是您的Text参数。 Not sure if the line breaks is only in the question, but, when we have a string in javascript, it must be on the same line. 不知道换行符是否仅在问题中,但是,当我们在javascript中有一个string时,它必须在同一行上。 You can't break lines with "enter". 您不能使用“输入”来换行。 You will need to concatenate the strings to do that, like this: 您需要连接strings来做到这一点,如下所示:

var html = '<div style="text-align: center;"><span style="font-style:italic;">Stavi dole' +
'</span><span style="font-weight: bold;">neki text<br><br><br>Hvala na paznji!' + 
'<br><br>Mozes Z da stavis<br><br>Ali Z<br><br>Hvala<br></span></div>';

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

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