[英]add quotation mark to the value by javascript
I would like the following text to appear as '"test test test'" using JavaScript. 我希望使用JavaScript将以下文本显示为““ test test test”“。
<body>
<blockquote>test test test</blockquote>
</body>
I have the following code but it does not work 我有以下代码,但它不起作用
<blockquote window.onload = function test()> test test test</blockquote>
<script type="text/javascript">
function test(){
var a = document.getElementsByTagName(blockquote).value
return "a";
}
</script>
One of these will work for you: 其中之一将为您工作:
All blockquotes on the page when all HTML elements are in the DOM : 当所有HTML元素都在DOM中时,页面上的所有块引用 :
<blockquote>test test test</blockquote>
<blockquote>test2 test2 test2</blockquote>
<script type="text/javascript">
window.onload = function(){
var bqs = document.getElementsByTagName('blockquote');
for(var i = 0; i < bqs.length; i++)
bqs[i].innerHTML = '"'+bqs[i].innerHTML+'"';
}
</script>
<blockquote>test3 test3 test3</blockquote>
All blockquotes on the page above the script : 脚本上方页面上的所有块引用 :
<blockquote>test test test</blockquote>
<blockquote>test2 test2 test2</blockquote>
<script type="text/javascript">
var bqs = document.getElementsByTagName('blockquote');
for(var i = 0; i < bqs.length; i++)
bqs[i].innerHTML = '"'+bqs[i].innerHTML+'"';
</script>
<blockquote>test3 test3 test3</blockquote>
Specific blockquote : 特定的块引用 :
<blockquote id="wrapinquotes">test test test</blockquote>
<blockquote>test2 test2 test2</blockquote>
<script type="text/javascript">
var el = document.getElementById('wrapinquotes');
el.innerHTML = '"'+el.innerHTML+'"';
</script>
<blockquote>test3 test3 test3</blockquote>
The reason your code wont work is because getElementsByTagName returns a list of all the tags, not just one tag. 您的代码无法正常工作的原因是因为getElementsByTagName返回所有标签的列表,而不仅仅是一个标签。 So you need to manipulate the list (array).
因此,您需要操作列表(数组)。
I found Paulpro's answer helpful, but there are no explanations, so this might help someone. 我发现Paulpro的回答很有帮助,但没有任何解释,因此可能会对某人有所帮助。
var bqs = document.getElementsByTagName('blockquote');
Get a list of all the blockquote tags. 获取所有blockquote标签的列表。 Call that list bqs
叫那个名单
for(var i = 0; i < bqs.length; i++)
Go through the list bqs once.And while you go down that list: 遍历列表bqs,然后在该列表中查找以下内容:
bqs[i].innerHTML = '"'+bqs[i].innerHTML+'"';
For each list item execute this code. 对于每个列表项,请执行此代码。
This code helps get from trying to execute on a list, to execute on all items on the list. 此代码有助于从尝试在列表上执行到对列表上的所有项目执行。 The last line can be changed to do other things to the list like:
bqs[i].style.display = 'none';
最后一行可以更改为对列表执行其他操作,例如:
bqs[i].style.display = 'none';
shouldnt it be 不应该
<script type="text/javascript">
function test(){
var element = document.getElementsByTagName("blockquote")[0]; //getElementsByTagName returns an array
var attr = document.createAttribute('value');
attr.nodeValue = element .value; //code to set your value.. you need to escape double quotes..
element .setAttributeNode(attr);
}
</script>
<blockquote window.onload = function test()> test test test</blockquote>
if you are trying change the text in the between thr tag then you need to use the innerHTML property.. 如果尝试更改thr标记之间的文本,则需要使用innerHTML属性。
function test(){
var element = document.getElementsByTagName("blockquote");
element.innetHTML='"'+element[0].innerHTML+'"';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.