zjm1126
zjm1126

Reputation: 35682

how to simulate $ function when can't use jquery

my iphone can't use jquery , so i have to simulate $ function by myself:

this is my code :

function $(str){
    var div=document.createElement('div')
    div.innerHTML=str;
    return div
   }

and you can see , the result has a Redundant parent div,

so how to clear the raw div ,

thanks

Upvotes: 1

Views: 171

Answers (3)

Geuis
Geuis

Reputation: 42277

What exact behavior(s) of $ are you trying to replicate. It does a lot:

$('#anid') //returns a DOM element reference if found
$('.aClass') //returns either one or more DOM element references

$('<a href="http://a.link.com">A Link</a>') // generates a DOM element and returns it

$(document) //passes a DOM reference into jQuery to be acted on

var a = document.getElementById('anid');
$(a) //passing in a DOM element reference for jQuery to act on.

So as you can see, $() does a lot. What exactly are you trying to achieve? Additionally, is this only for the iPhone or is the project you're working on expected to work on non-webkit browsers too?

Upvotes: 1

Chuck
Chuck

Reputation: 237070

I'd think you could instead return div.childNodes[0].

Upvotes: 1

kobe
kobe

Reputation: 15835

Once this function callisover

returndiv=$(str); this.div=returndiv document.body.appendChilde(returndiv)

this should make it work

Upvotes: 0

Related Questions