Javascript 查找并将内容分配给变量
我需要的是找到一个class=“selected”元素,然后将该元素的内容分配给一个变量Javascript 查找并将内容分配给变量,javascript,jquery,Javascript,Jquery,我需要的是找到一个class=“selected”元素,然后将该元素的内容分配给一个变量 <a href="#">This is the content</a> <a href="#" class="selected">This is the content</a> <a href="#">This is the content</a> 有什么帮助吗?谢谢使用: var elementContents=$('.sele
<a href="#">This is the content</a>
<a href="#" class="selected">This is the content</a>
<a href="#">This is the content</a>
有什么帮助吗?谢谢使用:
var elementContents=$('.selected').html()//假设在选定的类中只找到一个元素
希望这有帮助。使用:
var variable=$(".selected").text();
var elementContents=$('.selected').html()//假设在选定的类中只找到一个元素
希望这有帮助
var variable=$(".selected").text();
var someContent=$('') var someContent=$('') 给它一个Id,然后使用:
var content = document.getElementById("yourIdHere").innerHTML;
给它一个Id,然后使用:
var content = document.getElementById("yourIdHere").innerHTML;
为了确保只获取第一个实例,请不要忘记指示jQuery只获取第一个实例,并且为了确保它的可读性,我将使用.text()而不是.html()选择器,因为如果存在任何html,它将删除任何html
<a href="#">This is the content</a>
<a href="#" class="selected">This is the content</a>
<a href="#">This is the content</a>
<script type="text/javascript">
$(document).ready(function() {
var myVariable = $(".selected").first().text();
});
</script>
$(文档).ready(函数(){
var myVariable=$(“.selected”).first().text();
});
为了确保只获取第一个实例,请不要忘记指示jQuery只获取第一个实例,并且为了确保它的可读性,我将使用.text()而不是.html()选择器,因为如果存在任何html,它将删除任何html
<a href="#">This is the content</a>
<a href="#" class="selected">This is the content</a>
<a href="#">This is the content</a>
<script type="text/javascript">
$(document).ready(function() {
var myVariable = $(".selected").first().text();
});
</script>
$(文档).ready(函数(){
var myVariable=$(“.selected”).first().text();
});
您应该阅读关于jQuery的教程,以了解基本知识:文档也非常好:您应该阅读关于jQuery的教程,以了解基本知识:文档也非常好: