如何获取被点击元素的文本内容?

时间:2017-06-03 11:34:11

标签: php jquery

代码:

<script>
    $(document).ready(function(){
        $(".special").click(function(){
          courses = this.id;
          location.href = "courses-college.php?courses="+courses;
        });
    });
</script>

<ul>
    <li class="special" id="design">Design</li>
    <li class="special" id="forestry">Forestry</li>
    <li class="special" id="vetenerary">Vetenerary</li>
    <li class="special" id="diploma(engg)">Diploma(Engg)</li>
    <li class="special" id="BTechMTech">Dual Degree(Engg)</li>
    <li class="special" id="BBAMBA">Dual Degree(Mgmt)</li>
</ul>

在这个代码中,当我点击它时,逐个获取Id,这没关系,但我希望如果我点击并获得id ='BTechMtech',那么它将附加“&amp;”有链接,即

courses-college.php?courses=BtechMtech&Dual Degree(Engg)

类似于双学位(Mgmt)。那么,我该如何解决这个问题?请帮助。

谢谢

2 个答案:

答案 0 :(得分:1)

使用 encodeURIComponent 可帮助您转义元字符。

<script>
$(document).ready(function(){
    $(".special").click(function(){
      title = $(this).text();
      location.href = "courses-college.php?courses="+encodeURIComponent(title);
    });
});
</script>

答案 1 :(得分:0)

<script>
    $(document).ready(function(){
        $(".special").click(function(){
          courses = this.id;
          title = $(this).text();
          location.href = "courses-college.php?courses="+courses+"&title="+encodeURIComponent(title);
        });
    });
</script>

<ul>
    <li class="special" id="design">Design</li>
    <li class="special" id="forestry">Forestry</li>
    <li class="special" id="vetenerary">Vetenerary</li>
    <li class="special" id="diploma(engg)">Diploma(Engg)</li>
    <li class="special" id="BTechMTech">Dual Degree(Engg)</li>
    <li class="special" id="BBAMBA">Dual Degree(Mgmt)</li>
</ul>

https://jsfiddle.net/jzccb8dg/