简体   繁体   English

如何正确使用innerHTML

[英]How to properly use innerHTML

I have a link, that when I click it calls a javascript function with a string argument. 我有一个链接,当我单击它时,它会使用字符串参数调用javascript函数。 This string is actually an a-tag+script-tag, like: 该字符串实际上是一个a-tag + script-tag,例如:

<a>..</a><script>...</scipt>

which displays a video file. 显示视频文件。

So anyways, the javascript function is supposed to create that code, and make the video show on the page, but what shows up when I press the link is a string of the code, so its showing as text (but part of it is a hyperlink) and not executing to become the video. 因此,无论如何,应该使用javascript函数创建该代码,并将视频显示在页面上,但是当我按链接时显示的是代码的字符串,因此它显示为文本(但其中一部分是超链接)并且没有执行成为视频的操作。

Anyone know why? 有人知道为什么吗?

Its weird, because if I copy the code (which displays as text), and paste it in the editor like normal, then the video shows... 这很奇怪,因为如果我复制代码(以文本形式显示),然后像平常一样将其粘贴到编辑器中,则视频将显示...

<a title="Click to Show Video" href='javascript:void(0);' onclick="switchFunc('{$thisNode/@*[name()=current()/@Name]}');">
    <div dir="{@Direction}" class="ms-rtestate-field">
      <xsl:value-of select="$thisNode/@*[name()=current()/@Name]" disable-output-escaping="yes"/>
    </div>
</a>




<script type="text/javascript"> 
  function switchFunc(source) {
    document.getElementById('videoContainer').innerHTML = source;
  }  
</script>

<div id="videoContainer">    </div>

As I said in the comment I suspect that your attribute is being encoded, so you could try the following: 正如我在评论中所说,我怀疑您的属性已被编码,因此您可以尝试以下操作:

<a title="Click to Show Video" href='javascript:void(0);' >
<xsl:attribute name="onclick">switchFunc('<xsl:value-of select="$thisNode/@*[name()=current()/@Name]" disable-output-escaping="yes"/>');</xsl:attribute>
    <div dir="{@Direction}" class="ms-rtestate-field">
      <xsl:value-of select="$thisNode/@*[name()=current()/@Name]" disable-output-escaping="yes"/>
    </div>
</a>

Why dont you update the innerHtml with the tag you want, and have the javascript run via eval? 为什么不使用所需的标签更新innerHtml并通过eval运行javascript?

I am not entirely sure about the xslt part of it, but the idea would be to break up your two sections (you had said this is really an <a>Anchor</a> and <script>Javascript</script> ) 我不确定它的xslt部分,但想法是将您的两部分分开(您曾说过这实际上是<a>Anchor</a><script>Javascript</script>

html html

<a title="Click to Show Video" 
    href='javascript:void(0);' 
    onclick="switchFunc('<a>Anchor</a>','Javascript');">

js js

<script type="text/javascript"> 
  function switchFunc(htmlValue, javascriptMethodCall) {
    document.getElementById('videoContainer').innerHTML = htmlValue;
    eval(javascriptMethodCall);
  }  
</script>

Attribute values are always escaped regardless of output-escaping settings as the other way the content could break the wellformedness the XML file. 不管输出转义设置如何,总是对属性值进行转义,否则内容可能破坏XML文件的格式。

Anyway, the core architecture is the problematic on multiple ways. 无论如何,核心架构在很多方面都是有问题的。 First it moves some trivial client side functionality to the server. 首先,它将一些琐碎的客户端功能移至服务器。 Plus this way you encapsulate the main content of the page (video urls) into script code thus hide it from indexers. 加上这种方式,您就可以将页面的主要内容(视频URL)封装到脚本代码中,从而对索引器隐藏它。

I am a big fun of XSLT but the weirdo look and hard readability of the code tells that something is not OK. 我对XSLT很有兴趣,但是代码的怪异外观和难以理解的代码告诉我们有些问题。

A possible solution is the annotation approach: render lists with items holding key information stored as data- attribute on them and let the javascript code (your own or a client side template engine) build the output. 一种可能的解决方案是注释方法:渲染列表,其中包含项的关键信息作为数据属性存储在其上,然后让javascript代码(您自己的代码或客户端模板引擎)构建输出。

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

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