简体   繁体   中英

How do i use this javascript variable properly?

I am trying to expand on some code written by someone else, but I am having trouble using one of the javascript variables. If I set it in the title of a div or something similar like so:

$("#test12").attr("title" , ccode);

then it works fine and the title of the div is 'CA', which it should be. But if I try to put it into the text area of the div, then it tries to run a function or something but I can't see where it's doing it.

Is there a way I can convert it to simple text and stop it from running any functions?

Thanks for any help

Edit:

This is all of the code I can see at the moment:

<script>

//<!--
function loadForeignOffices(ccode){

    //load window with details...
    $('#iframe_3').attr("src", "<?php echo $html->url("/", true); ?>erc/maps/contacts/"+ccode);
    $("#test12").attr("title" , ccode);



}
//-->
</script>

Basically what I'm trying to do is use the ccode variable because I want to display CA on the screen, but when I try to do that it seems to run some other function and fails, and doesnt show CA.

Keep in mind your PHP code is executing first on the server, then your javascript.

Here's a JS Fiddle as a start: http://jsfiddle.net/QsFJ4/3/

You may need to brush up on your jQuery dot-operators:

.load() is an ajax call that will load the contents of () into the selected element. In one of your comments you mention you are trying to do this $("#test12").load(ccode); which is inherently silly, if ccode is the string "CA"

.attr() will change the attributes of the selected element. If you are trying to display "CA", $("#test12").attr("title", ccode); this will clearly not accomplish that -- As "CA" is added to the title attribute. As in <div id="test12" title="CA"></div>

Perhaps what you are looking for is .html() which inserts the string into the selected element. Or even .innerText()

Why don't you spend some time reading the jQuery documentation . A little bit goes a long way!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM