开发者

Arrange in list?

i have code

function mywidgets(){
  alert(id1);
  alert("ABC"+"     "+url1);
  var ni = document.getElementById('Listsample');
  var numi = document.getElementById('cr');
  var newdiv = document.createElement('li');
  newdiv.innerHTML = "&开发者_StackOverflow社区lt;a href='#' onclick='add("+url1+");' id='cricket' name='cricket'>"+id1+"</a>";
  ni.appendChild(newdiv);
} 

in which i want to add url and id one by one in list li. Is this is correct or some modifications is required? I tried many ways but nothing worked. Please suggest some solution.

Thanks.


"nothing worked" - do you see any javascript errors?

Where is "id1", "url1" etc defined? Are they defined at all?

The following works for me:

<html>
<head>
    <script>
        function mywidgets(){
            var list = document.getElementById('sampleList');
            var newListItem = document.createElement('li');
            var url="www.google.com/search?q=cricket";
            newListItem.innerHTML = "<a href='#' onclick='add(\"+url+\")' id='cricket' name='cricket'>Cricket</a>";
            list.appendChild(newListItem);
        }
    </script>
</head>
<body>
    <ul id="sampleList">
    <li>Test</li>
    </ul>
    <button onclick="mywidgets()">Add</button>
</body>

Try to have better variable names. Try to see javascript errors in your browser.

Also: If you can, use firefox. Firefox with the addon Firebug is be very useful in debugging javascript.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜