Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向HTML元素添加Javascript变量_Javascript_Firefox_Greasemonkey - Fatal编程技术网

向HTML元素添加Javascript变量

向HTML元素添加Javascript变量,javascript,firefox,greasemonkey,Javascript,Firefox,Greasemonkey,因此,我有一些代码可以做四件事: 从每个标题中删除“.mp4”扩展名 更改我的视频类别 在所有视频中添加相同的描述 在所有视频中输入相同的关键字 注意:所有这些都将在YouTube上传页面上完成。我在Mozilla Firefox中使用Greasemonkey 我写了这个,但我的问题是:如何将实际HTML页面中的HTML标题更改为新标题(这是一个Javascript变量) 这是我的代码: function remove_mp4() { var title = document.getE

因此,我有一些代码可以做四件事:

  • 从每个标题中删除“.mp4”扩展名
  • 更改我的视频类别
  • 在所有视频中添加相同的描述
  • 在所有视频中输入相同的关键字
注意:所有这些都将在YouTube上传页面上完成。我在Mozilla Firefox中使用Greasemonkey

我写了这个,但我的问题是:如何将实际HTML页面中的HTML标题更改为新标题(这是一个Javascript变量)

这是我的代码:

function remove_mp4()
{
   var title = document.getElementsByName("title").value;
   var new_title = title.replace(title.match(".mp4"), "");
}
function add_description()
{
   var description = document.getElementsByName("description").value;
   var new_description = "Subscribe."
}
function add_keywords()
{
   var keywords = document.getElementsByName("keywords").value;
   var new_keywords = prompt("Enter keywords.", "");
}
function change_category()
{
   var category = document.getElementsByName("category").value;
   var new_category = "<option value="27">Education</option>"
}
remove_mp4();
add_description();
add_keywords();
change_category();
函数删除_mp4()
{
var title=document.getElementsByName(“title”).value;
var new_title=title.replace(title.match(“.mp4”),”);
}
函数add_description()
{
var description=document.getElementsByName(“description”).value;
var new_description=“订阅”
}
函数add_关键字()
{
var关键字=document.getElementsByName(“关键字”).value;
var new_keywords=提示(“输入关键字”。,”);
}
功能更改\u类别()
{
var category=document.getElementsByName(“category”).value;
var new_category=“教育”
}
取下_mp4();
添加描述();
添加_关键字();
更改_类别();
注意:如果您看到JavaScript代码中有任何错误,请告诉我


注2:如果您想知道为什么我将当前HTML值存储在变量中,那是因为我认为我必须使用它们来替换HTML值(我可能错了)。

Sry,但您的问题不太清楚。您所指的HTML标题到底是什么

如果是要修改的元素,请使用以下命令:

element.setAttribute('title', 'new-title-here');
如果要修改窗口标题(显示在浏览器选项卡中),可以执行以下操作:

document.title = "the new title";
元素是不可见的,它仅间接显示在窗口或选项卡标题中。这意味着您希望更改窗口/选项卡标题中显示的内容,而不是HTML代码本身。您可以通过更改以下选项来执行此操作:


您已经从
.value
属性中读取了元素,因此也应该将其写回:

document.getElementsByName("title").value = new_title

如果要更改名为title的元素中的文本内容,请尝试使用
innerHTML

 var title = document.getElementsByName("title").value;
 document.getElementsByName("title").innerHTML = title.replace(title.match(".mp4"), "");

来源:

我想你的问题只是关于标题的改变,而不是关于其他的;另外,我假设您的意思是更改文档中所有“title”为
name
属性的元素,而不是文档标题

在这种情况下,您确实可以使用
document.getElementsByName(“title”)

要处理
name=“title”
元素,可以执行以下操作:

titleElems=document.getElementsByName("title");
for(i=0;i<titleElems.length;i++){
    titleInner=titleElems[i].innerHTML;
    titleElems[i].innerHTML=titleInner.replace(titleInner.match(".mp4"), "");
}
我对关键词不是很确定(我现在还没有一个YouTube页面在我面前),所以这假设它是一个文本字段/区域,就像描述:

document.getElementsByName("keywords")[0].value=prompt("Please enter keywords:","");
同样,根据您的问题,该问题只设置了类别thingy的
.value

document.getElementsByName("description")[0].value="<option value='27'>Education</option>";
document.getElementsByName(“说明”)[0].value=“教育”;
但是,在最后一个例子中,请注意,我将
“27”
更改为
'27'
:如果双引号字符串的处理方式与其他任何字符一样,则不能将双引号放在双引号字符串中:)


这有帮助吗?:)

已经介绍了很多内容,但我仍然想提醒您,如果您正在寻找跨浏览器兼容性,innerHTML是不够的,因为您可能也需要innerText或textContent来处理一些旧版本的IE,甚至需要使用其他方法来修改元素的内容

作为旁注,innerHTML被大多数人认为是不推荐的,尽管有些人仍然使用它。(我不是在这里讨论使用它好还是不好,但这只是一个小评论,请大家检查一下)

关于备注,我建议通过创建一些更通用的版本进行编辑或添加,以尽量减少您创建的函数数量,例如,您可以执行以下操作:

/*
*  @param $affectedElements the collection of elements to be changed      
*  @param $attribute here means the attribute to be added to each of those elements
*  @param $attributeValue the value of that attribute
*/
function add($affectedElements, $attribute, $attributeValue){
  for(int i=0; i<$affectedElements.length; i++){
    ($affectedElements[i]).setAttribute($attribute, $attributeValue);
  }
}
/*
*@param$affectedElements要更改的元素集合
*@param$attribute在这里表示要添加到每个元素的属性
*@param$attributeValue该属性的值
*/
函数添加($affectedElements、$attribute、$attributeValue){

对于(int i=0;我想修改标题,这是一个HTML元素。我是在“element.setAttribute”中使用“element”,还是用我的元素名替换“element”…例如,这样它就会是trash.setAttribute('title','new_title'))
element
是一个保存对dom对象引用的变量。因此,您可以用类似于
document.getElementById(“title”).setAttribute(“key”,“value”)的内容替换element
是的,谢谢。然后我们得到了add\u描述函数。它的作用是,只需更改空白描述框即可(description是一个输入HTMl标签的name属性)添加到我的默认值中,即“Subscribe”。之后,我们得到add_keywords()。首先,我将输入我想要的关键字,然后它将更改默认值,空白关键字(同样是name属性)与我的关键字一起更改。最后,更改_category()我会选择教育作为我的Youtube类别。希望我能更清楚一点。:)顺便说一句,我在生成的
标题元素
的每个元素上循环的原因是
文档。getElementsByName
返回一个数组(注意名称,“Elements”)。你不能只作为元素访问数组:)我编辑了我的答案来处理问题的其他部分,这有帮助吗?(顺便说一句,很抱歉回答稍晚,我花时间写了我的扩展答案:P)我创建了一个新的脚本,网站的源代码在这里:。你能回顾一下并告诉我它可以吗(我的代码)。谢谢。我提供了关于您链接的问题的答案;不过,如果没有真正的原因,请不要将问题转换为新问题;您可以将示例HTML代码放入此问题中。无论如何,这不是一个坏主意
document.getElementsByName("description")[0].value="<option value='27'>Education</option>";
/*
*  @param $affectedElements the collection of elements to be changed      
*  @param $attribute here means the attribute to be added to each of those elements
*  @param $attributeValue the value of that attribute
*/
function add($affectedElements, $attribute, $attributeValue){
  for(int i=0; i<$affectedElements.length; i++){
    ($affectedElements[i]).setAttribute($attribute, $attributeValue);
  }
}
var category = document.getElem.... . options[put-index-here];
//JavaScript also lets you create <option> elements with the Option() constructor