I want to add href to an a html element from class of another element. It might also be good to know that i am using Django.As well i dont want the second url to be visible, when the character is not selected yet. Firstly i have this list:
<ul>
<a class="1" id="tablink">A</a>
<a class="2" id="tablink">B</a>
...
</ul>
if list from the first ul is selected, i want the class of the list (example: A) to add to href element to all links in another ul:
<ul>
<a href="{% url id=id character= <!--here i want the number selected to be inserted--> %}">One</a>
<a href="{% url id=id character= <!--here i want the number selected to be inserted--> %}">Two</a>
...
</ul>
Thank you! EDIT: I have this code now:
<script type="text/javascript">
$(".tablinks").on("click", function(){
var char = this.getAttribute("id");
document.getElementById("subjects").href = "{% url 'subject_id' letnik_id=letnik_id classes_id=classes_id subject_id=" + char + " %}";
});
</script>
But it has a problem: It only works if i add attribute to element that i got with ID, so id doesnt work with
<a class="subjects">Hello</a>
doucment.getElementsByClassName("subjecs").href = ...
and the DJango url doesnt work ("{% url 'subject_id' letnik_id=letnik_id classes_id=classes_id subject_id=" + char + " %}"
).
答案 0 :(得分:1)
使用anchor
href
使用:
$("#yourElement").setAttribute("href", "http://yourlink.com");
如果你正在使用JavaScript:
document.getElementById("yourElement").href="http://yourlink.com"";
您使用该代码禁用第二个锚点,更改ID和类值,您不能拥有相同的id
- id
是唯一值:
$(".tablink").on("click", function(){
$(".tablink").css("display", "none");
$(this).css("display", "block");
});
要在点击链接时添加href
,只需添加到上述功能的底部:
$(this).setAttribute("href", "http://yourlink.com");