我有很多div与publish_0我想要改变的类publish_1点击一个按钮.
现在我使用它但它只改变一个项目.
如何将setattribute应用于具有该属性的所有项目publish_0.
document.querySelector('.publish_0').setAttribute("class", "publish_1");
Run Code Online (Sandbox Code Playgroud) 可能重复:
JavaScript setAttribute vs .attribute =
javascript dom,如何处理"特殊属性"作为属性?
很多时候,在Usenet的论坛或地方,我被一些人(当批评我的代码时)告诉我,而不是说,例如var link = a.href我应该使用var link = a.getAttribute('href');.并且在想要分配时使用其互补的setAttribute().
他们说这是正确的做法,我错了,等等等等......我通常不会注意那些.当我问为什么没有人给出真正的答案时.
现在我很好奇在哪些情况下更适合使用其中一种或另一种.
在什么情况下更适合说var link = a.getAttribute('href');而不是var link = a.href?
在什么情况下,我使用setAttribute()来分配,而不是直接通过其标识符为成员赋值?即:`a.href ='someURL';
我不知道以下两个javascript/css代码如何产生不同的结果:
第一名:
prev.setAttribute('style', 'position:absolute;left:-70px;opacity:0.4;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;');
Run Code Online (Sandbox Code Playgroud)
第二:
prev.setAttribute('style', 'opacity:0.4;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;');
prev.setAttribute('height', size);
prev.setAttribute('width', size);
prev.setAttribute('id', 'thumb'+i);
prev.setAttribute('position', 'absolute');
prev.setAttribute('left', '-70px');
Run Code Online (Sandbox Code Playgroud)
在第二个中,位置和左侧完全被忽略.具有2行代码而没有代码的结果是相同的.
它只适用于我放置prev.style.left,同样的位置.但是setAttribute适用于高度和宽度.我真的需要知道原因
我有以下HTML:
<input class="vertical-tabs-active-tab" type="hidden" value="account" name="tabs__active_tab">
Run Code Online (Sandbox Code Playgroud)
我需要JavaScript将"帐户"的值更改为"yolo".
我以为我能做到这一点:
document.getElementsByClassName('vertical-tabs-active-tab').setAttribute("value", "yolo");
Run Code Online (Sandbox Code Playgroud)
但是这会产生以下错误:
document.getElementsByClassName(...).setAttribute is not a function
Run Code Online (Sandbox Code Playgroud)
我无法在输入中添加"id",我必须根据类更改值.
我正在使用Raphael在网站上绘制一些元素.元素包括矩形,线(路径).我给了path元素一个id,并尝试在该行的onclick事件中访问它.但是当我对id进行警报时,看不到任何东西.以下是代码段
function createLine()
{
var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight);
t.attr('stroke-width','3');
t.attr('id','Hello');
t.node.onclick = processPathOnClick;
}
function processPathOnClick()
{
alert($(this).attr("id"));
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我上面的代码有什么问题.任何指针都会有所帮助.
谢谢
因为虽然我无法使用type = text设置输入字段的值.之前,我总是使用这样的东西:
<input style="display: none" type="text" name="geo_poi" value="" id="geofeld" />
Run Code Online (Sandbox Code Playgroud)
然后,在JavaScript中我添加了包含如下行的代码:
document.getElementById("geofeld").value = geo_poi;
Run Code Online (Sandbox Code Playgroud)
这总是奏效的.也许新浏览器不再支持上面的方法了.
我是新手SVG,所以我提前为我的无知道歉.
我创造了一个小提琴,只是玩弄东西.http://jsfiddle.net/a46p8/
var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
svg.setAttribute('width','200');
svg.setAttribute('height','200');
var line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
line.setAttribute('width', '0');
line.setAttribute('height', '0');
line.setAttribute('x1', '0');
line.setAttribute('y1', '0');
line.setAttribute('x2', '150');
line.setAttribute('y2', '150');
line.setAttribute('stroke', 'rgb(255,0,0)');
line.setAttribute('stroke-width', '2');
svg.appendChild(line);
var ct = document.getElementById('container');
ct.appendChild(svg);
Run Code Online (Sandbox Code Playgroud)
setAttributes真的没有更简单的方法吗?例如,是否可以将它们与以下内容组合:
line.setAttribute('x1', '0', 'y1', '0', 'x2', '150', 'y2', '150');
Run Code Online (Sandbox Code Playgroud)
是的,我知道当我尝试小提琴时它不起作用.但有没有办法做到这一点?如果没有,你不能的原因是什么?
我是javascript的新手,想要编写一个html网站的标题.JS:当窗口宽度小于1215px时 - >左边部分变为100%宽度; 标题的右侧部分为0
我总是得到"无法读取属性'setAttribute'的null"错误!
请帮忙!码:
if (window.innerWidth < 1215) {
document.getElementById("#headerLeft").setAttribute("style", "width:100%");
document.getElementById("#headerRight").setAttribute("style", "width:0%");
} else {
document.getElementById("#headerLeft").setAttribute("style", "width:50%");
document.getElementById("#headerRight").setAttribute("style", "width:50%");
}Run Code Online (Sandbox Code Playgroud)
body {
margin:0;
}
header{
height:100px;
width:100%;
}
#headerLeft {
background-color:#FF7043;
height:100px;
float:left;
}
#headerRight {
background-color:#FFFFFF;
height:100px;
float:right;
}
.headerTitle {
font-family:'Roboto', sans-serif;
margin:15px;
font-size:70px;
}
#headerRight .headerTitle {
text-align:left;
color:#FF7043;
font-weight:300;
}
#headerLeft .headerTitle {
text-align:center;
color:#FFFFFF;
font-weight:900;
}Run Code Online (Sandbox Code Playgroud)
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>example</title>
<!-- css -->
<link type="text/css" rel="stylesheet" href="style.css">
<!-- fonts …Run Code Online (Sandbox Code Playgroud)所以,我知道这个问题已经得到了回答,但是之前的答案都没有能让我的代码工作。我有一个 html 结构如下:
<div class="form">
<div class="formrow">
<div class="previewcontainer">
<object id="preview">
<object>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我试图将数据属性设置为对象,如下所示:
var link = "http://www.someurl.com";
var preview = document.querySelectorAll ("#preview");
preview.setAttribute("data", link);
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误preview.setAttribute is not a function
两个问题合二为一:鉴于在 Julia 中绘制的一条线,我如何才能
作为下面代码中的一个具体示例,我如何 1. 删除以前的回归线或 2. 将它们的不透明度更改为 0.1?
using Plots; gr()
f = x->.3x+.2
g = x->f(x)+.2*randn()
x = rand(2)
y = g.(x)
plt = scatter(x,y,c=:orange)
plot!(0:.1:1, f, ylim=(0,1), c=:green, alpha=.3, linewidth=10)
anim = Animation()
for i=1:200
r = rand()
x_new, y_new = r, g(r)
push!(plt, x_new, y_new)
push!(x, x_new)
push!(y, y_new)
A = hcat(fill(1., size(x)), x)
coefs = A\y
plot!(0:.1:1, x->coefs[2]*x+coefs[1], c=:blue) # plot new regression line
# 1. delete previous line
# 2. set alpha …Run Code Online (Sandbox Code Playgroud) setattribute ×10
javascript ×9
css ×2
html ×2
attributes ×1
getattribute ×1
html5 ×1
input ×1
jquery ×1
julia ×1
members ×1
object ×1
plot ×1
raphael ×1
svg ×1