2016-11-03 9 views
0

JavaScriptでhtmlを作成しようとしています。アンカータグへのリンクが期待通りに機能していません。アンカータグのJavascript動的生成が機能していません

var test ="http://blogs.edweek.org/edweek/finding_common_ground/2016/10/if_coaching_is_so_powerful_why_are't_principals_being_coached.html?cmp=SOC-EDIT-FB"; 

    var res ="<a style='color:#7Bc76B;text-decoration: none;' target='_blank' href='"+test+"'>Link to read more ></a>"; 

    document.getElementById("res").innerHTML = res; 

https://jsfiddle.net/b4kqsn6u/

すべてのヘルプは理解されるであろう。

おかげ

+0

質問を解析され得るしない文字列を行いますダブルquote.the外の単一引用符を入れます? ")には、目的の振る舞い、特定の問題またはエラー、および質問自体にそれを再現するのに必要な最短のコードが含まれていなければなりません。明確な問題文がない質問は、他の読者にとって有用ではありません。参照:[mcve]を作成する方法。 – Tibrogargan

+0

何が問題なのですか?簡単に例を挙げてください。 –

+0

リンクが動作しません。 –

答えて

2

あなたは、単にあなたの<a>宣言を変更することができます:あなたは(encodeURIは、単一引用符をコードしない)%27で単一引用符を置き換えることによって、あなたのURLをいずれかの事前URLENCODEまたは手動でそれを行うことを確認してくださいあなたのURLに存在する:

var res ='<a style="color:#7Bc76B;text-decoration: none;" target="_blank" href="'+test+'">Link to read more ></a>'; 

は、上記のように単一引用符内で宣言し、すべての属性を二重引用符内にある

https://jsfiddle.net/b4kqsn6u/3/

0

あなたはそこに属していない、あなたのURLで単一引用符( ')を持っています。単一引用符の世話をするために、次のように

var test ="http://blogs.edweek.org/edweek/finding_common_ground/2016/10/if_coaching_is_so_powerful_why_aren't_principals_being_coached.html?cmp=SOC-EDIT-FB"; 

test = test.replace(/'/g, "%27"); 

var res ="<a style='color:#7Bc76B;text-decoration: none;' target='_blank' href='"+test+"'>Link to read more ></a>"; 

document.getElementById("res").innerHTML = res; 
+0

http://blogs.edweek.org/edweek/finding_common_ground/2016/10/if_coaching_is_so_powerful_why_are%27t_principals_being_coached.html?cmp=SOC-EDIT- FBこれは動作していません –

+1

これは動作しています。元のURLは、http://blogs.edweek.org/edweek/finding_common_ground/2016/10/if_coaching_is_so_powerful_why_are't_principals_being_coached.html?cmp=SOC-EDIT-FBを指しています。これはあなたに404を提供します。あなたが期待していたものを得る;-)あなたが以前に持っていたエラーは、href属性が途中で終了したため、一重引用符があなたのURLを混乱させたということでした。 –

+0

http://blogs.edweek.org/edweek/finding_common_ground/2016/10/if_coaching_is_so_powerful_why_are't_principals_being_coached.html?cmp=SOC-EDIT-FBこれは動作しています –

0

私は通常、単一引用符は、デバッグのヘルプ(「なぜこのコードは動作していないを求めて

var test ="http://blogs.edweek.org/edweek/finding_common_ground/2016/10/if_coaching_is_so_powerful_why_are't_principals_being_coached.html?cmp=SOC-EDIT-FB"; 
 

 
var res ='<a style="color:#7Bc76B;text-decoration: none;" target="_blank" href="'+test+'">Link to read more ></a>'; 
 

 
document.getElementById("res").innerHTML = res;
<div id="res"> 
 

 
</div>

関連する問題