<legend id='8Y4kz'><style id='8Y4kz'><dir id='8Y4kz'><q id='8Y4kz'></q></dir></style></legend>

<small id='8Y4kz'></small><noframes id='8Y4kz'>

    • <bdo id='8Y4kz'></bdo><ul id='8Y4kz'></ul>

    1. <i id='8Y4kz'><tr id='8Y4kz'><dt id='8Y4kz'><q id='8Y4kz'><span id='8Y4kz'><b id='8Y4kz'><form id='8Y4kz'><ins id='8Y4kz'></ins><ul id='8Y4kz'></ul><sub id='8Y4kz'></sub></form><legend id='8Y4kz'></legend><bdo id='8Y4kz'><pre id='8Y4kz'><center id='8Y4kz'></center></pre></bdo></b><th id='8Y4kz'></th></span></q></dt></tr></i><div id='8Y4kz'><tfoot id='8Y4kz'></tfoot><dl id='8Y4kz'><fieldset id='8Y4kz'></fieldset></dl></div>

      1. <tfoot id='8Y4kz'></tfoot>
      2. img 标签的 HTML 跨域属性

        时间:2023-10-14
      3. <tfoot id='2aHXo'></tfoot>

          • <bdo id='2aHXo'></bdo><ul id='2aHXo'></ul>
            1. <legend id='2aHXo'><style id='2aHXo'><dir id='2aHXo'><q id='2aHXo'></q></dir></style></legend>
                <tbody id='2aHXo'></tbody>

              <small id='2aHXo'></small><noframes id='2aHXo'>

                <i id='2aHXo'><tr id='2aHXo'><dt id='2aHXo'><q id='2aHXo'><span id='2aHXo'><b id='2aHXo'><form id='2aHXo'><ins id='2aHXo'></ins><ul id='2aHXo'></ul><sub id='2aHXo'></sub></form><legend id='2aHXo'></legend><bdo id='2aHXo'><pre id='2aHXo'><center id='2aHXo'></center></pre></bdo></b><th id='2aHXo'></th></span></q></dt></tr></i><div id='2aHXo'><tfoot id='2aHXo'></tfoot><dl id='2aHXo'><fieldset id='2aHXo'></fieldset></dl></div>
                  本文介绍了img 标签的 HTML 跨域属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试了解如何为 img 标签使用 crossorigin 属性.我找不到一个很好的例子(我发现的关于启用 CORS 的图像是用 JavaScript 代码解释的,因此我看不到带有 img 标签的 crossorigin 属性.

                  I am trying to understand how to use the crossorigin attribute for the img tag. I couldn't find a good example (The ones I found about CORS enabled images are explained with JavaScript codes, therefore I couldn't see the crossorigin attribute with the img tag.

                  我有一个猜测,如果我理解错误,请纠正我的错误.

                  I have got a guess, please correct my mistakes if I understood something wrong.

                  首先可以编写下面的代码来将图像绘制到画布上:

                  First of all one can write the code piece below to draw an image to canvas:

                  <canvas id="canvas" width=400 height=400></canvas>
                  <br><br>
                  <img id="image" src="http://...." alt="" width="400" height="400">
                  <script>
                  function draw() {
                      var canvas = document.getElementById("canvas");
                      var context = canvas.getContext("2d");
                      var img = new Image();
                      img.crossOrigin = "Anonymous";
                      img.src = document.getElementById("image").value;
                      context.drawImage(img, 40, 40);
                  }
                  </script>
                  

                  下面的代码是否等同于上面的代码?它不包含img.crossOrigin",但在 img 标签中有 crossorigin 属性.

                  Is the code below equivalent to the upper one? It doesn't include "img.crossOrigin" but have crossorigin attribute in the img tag.

                  <canvas id="canvas" width=400 height=400></canvas>
                  <br><br>
                  <img id="image" crossorigin="anonymous"src="http://...." alt="" width="400" height="400">
                  <script>
                  function draw() {
                      var canvas = document.getElementById("canvas");
                      var context = canvas.getContext("2d");
                      var img = new Image();
                      img.src = document.getElementById("image").value;
                      context.drawImage(img, 40, 40);
                  }
                  </script>
                  

                  说实话,我无法进行实验,因为我不知道哪个网站允许将其图像用作 CORS.

                  To tell the truth I cannot make experiments because I don't know what site allows to use its images as CORS.

                  我的猜测是,如果 CORS 请求是由匿名完成的,如果站点允许在画布中使用其图像,您可以在画布中绘制它,如果不是,即使请求是匿名完成的,您也无法在画布中绘制它(我不确定我是否在这里).因此,上述两个示例都必须匿名请求 CORS.

                  What I guess is that, if a site allow to use its images in canvas if the CORS request is done by anonymously you can draw it in canvas, if not you cannot draw it in canvas even if the request is done by anonymously (I am not sure if I am right here). Therefore both of the examples above must be requesting CORS anonymously.

                  你能说如果他们两个工作相同吗?如果不是,您能否解释一下原因并给我一个使用带有 img 标签的 crossorigin 属性的示例?

                  Could you please say if both of them works the same? If not, could you please explain why and give me an example using the crossorigin attribute with the img tag?

                  推荐答案

                  由于您使用#image 元素作为图像的来源,因此您的代码的两个版本大致相同.

                  Since you are using the #image element as the source for your image, the 2 versions of your code are roughly equivalent.

                  但是...

                  img 元素中没有 crossorigin="anonymous" 的版本可能仍然会产生跨域违规.

                  The version without crossorigin="anonymous" in the img element will probably still generate a cross-domain violation.

                  这是因为图像最初加载到 img 元素中没有将跨域标志设置为匿名.

                  That's because the image is originally loaded into the img element without the cross-origin flag set to anonymous.

                  javascript 代码可能会使用来自 img 元素的图像的缓存版本,而不是尝试从 http://...

                  The javascript code will likely use the cached version of the image from the img element rather than trying to reload it from http://...

                  这意味着缓存的图像数据仍会将画布污染为包含跨域内容.

                  This means the cached image data will still taint the canvas as containing cross-origin content.

                  顺便说一句,您的代码中有语法错误:

                  BTW, a syntax error in your code:

                  // Not:  img.src = document.getElementById("image").value;
                  
                  img.src = document.getElementById("image").src;
                  

                  这篇关于img 标签的 HTML 跨域属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:跨域 Ajax 请求在 Opera 和 IE9 中不起作用? 下一篇:Javascript CORS - 不存在“Access-Control-Allow-Origin&quo

                  相关文章

                  最新文章

                  <legend id='0Hkzp'><style id='0Hkzp'><dir id='0Hkzp'><q id='0Hkzp'></q></dir></style></legend>

                    <i id='0Hkzp'><tr id='0Hkzp'><dt id='0Hkzp'><q id='0Hkzp'><span id='0Hkzp'><b id='0Hkzp'><form id='0Hkzp'><ins id='0Hkzp'></ins><ul id='0Hkzp'></ul><sub id='0Hkzp'></sub></form><legend id='0Hkzp'></legend><bdo id='0Hkzp'><pre id='0Hkzp'><center id='0Hkzp'></center></pre></bdo></b><th id='0Hkzp'></th></span></q></dt></tr></i><div id='0Hkzp'><tfoot id='0Hkzp'></tfoot><dl id='0Hkzp'><fieldset id='0Hkzp'></fieldset></dl></div>
                  1. <small id='0Hkzp'></small><noframes id='0Hkzp'>

                      <bdo id='0Hkzp'></bdo><ul id='0Hkzp'></ul>

                    <tfoot id='0Hkzp'></tfoot>