如何将带有javascript的html代码嵌入到我的网页中? [英] How to Embed html code with javascript, into my webpage?

查看:153
本文介绍了如何将带有javascript的html代码嵌入到我的网页中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我试图找出一种在我的纯HTML页面中嵌入代码的方法.它以用户代码为例

Hi all.
I am trying to figure out a way to embed a code in my plain html page. It takes the code from the user for example

<!-- TradingView Chart BEGIN -->
<script type="text/javascript" src="https://s3.amazonaws.com/tradingview/tv.js"></script>
<script type="text/javascript">
var tradingview_embed_options = {};
tradingview_embed_options.width = 640;
tradingview_embed_options.height = 400;
tradingview_embed_options.chart = 'vkcQRj1Z';
new TradingView.chart(tradingview_embed_options);
</script>
<p><a href="https://www.tradingview.com/chart/XOM/vkcQRj1Z-Consolidate/">Consolidate</a> by <a href="https://www.tradingview.com/u/lvn2007/">lvn2007</a> on <a href="https://www.tradingview.com/">TradingView.com</a></p>
<!-- TradingView Chart END -->



并将其注入到我的网页中,以显示该代码所代表的图表,如您在javascript中所见,因此,我正在寻找一种无需刷新或转到新页面来触发此javascript的解决方案,现在哪个不起作用.
谢谢.



and inject it into my webpage, to show the chart which this code represent as you can see its in javascript, so i am looking for a solution where i don''t have to refresh or go to the new page to trigger this javascript, which right now is not working.
Thanks.

推荐答案

我不知道您为什么要使用某些Amazon库(或类似的库;您可以问他们如何使用它),但是存在问题在当前页面中嵌入"任何HTML代码非常简单.例如,尝试以下操作:
I have no idea why would you use some Amazon library (or whatever it is; you can ask them how to use it), but the problem of "embedding" any HTML code in the current page is ridiculously simple. Try, for example, this:
document.body.innerHTML = "<h1>Write here any content you want!</h1>


自然地,您可以将任何东西嵌入"到任何给定的HTML元素中.如果您不知道如何找到所需的元素,则可以特别使用document.getElementById.要转换HTML而不仅仅是编写HTML,您可以读取innerHTML属性的值,对其进行分析并编写一个新值.因此,最终,您可以编写任何您想要的东西.

还有许多转换文档DOM的更精细方法,但是我宁愿推荐jQuery: http://api.jquery.com /category/manipulation [ ^ ].

如果您需要学习jQuery(强烈推荐),请参见:
http://en.wikipedia.org/wiki/JQuery
http://jquery.com
http://learn.jquery.com
http://learn.jquery.com/using-jquery-core
http://learn.jquery.com/about-jquery/how-jquery-works (从这里开始).

—SA


Naturally, you can "embed" anything in any given HTML element. If you don''t know how to locate the element you need, you can use document.getElementById, in particular. To transform HTML instead of just writing it, you can read the value of the innerHTML property, analyze it and write a new value. So, ultimately, you can write anything your want.

There are many more fine-grain method of transforming your document DOM, but I would rather recommend jQuery: http://api.jquery.com/category/manipulation[^].

If you need to learn jQuery (highly recommended), please see:
http://en.wikipedia.org/wiki/JQuery,
http://jquery.com,
http://learn.jquery.com,
http://learn.jquery.com/using-jquery-core,
http://learn.jquery.com/about-jquery/how-jquery-works (start from here).

—SA


HI.

这是您的解决方案.

我对您的代码进行了以下更改

1).添加对jquery库的引用.

2).保存参考图表类
var c1 = new TradingView.chart(tradingview_embed_options);

3).调用渲染方法
c1.render();

4).使用jquery删除多余的链接

HI.

Here is your solution.

I did following changes to your code

1). Add reference to jquery library.

2). Save a reference chart class
var c1=new TradingView.chart(tradingview_embed_options);

3). Call render method
c1.render();

4). Remove extra link using jquery


("div.tradingview-widget a").remove();


更改的代码如下.

("div.tradingview-widget a").remove();


Changed code is as under.


这篇关于如何将带有javascript的html代码嵌入到我的网页中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆